iOS一個(gè)簡(jiǎn)單的圖片預(yù)覽

前言

公司項(xiàng)目有很多地方要用到圖片上傳功能,還有上傳的歷史記錄,這方面就有需要預(yù)覽這個(gè)需求啦,之前做的都沒(méi)有這個(gè)功能,于是夏夏自己寫(xiě)了個(gè)簡(jiǎn)易的預(yù)覽。。。

照片預(yù)覽demo.gif

由于是模擬器,沒(méi)有辦法把放大,縮小等功能展示出來(lái)。

思路

1.只是簡(jiǎn)單的實(shí)現(xiàn)下預(yù)覽的功能,于是夏夏只是單純的寫(xiě)了個(gè)view用于展示,一般的預(yù)覽都是全屏展示的,而且長(zhǎng)按的時(shí)候要能保存圖片,也就是說(shuō)要presentsheet出來(lái),于是夏夏就添加到NavigationControllerView上了。當(dāng)然,為了美觀,夏夏也是加了個(gè)專場(chǎng)動(dòng)畫(huà)~ show code 思密達(dá):

UIImageView *imageView = (UIImageView *)tap.view;
UIImage *image = imageView.image;
ReViewPhotoView *review = [[ReViewPhotoView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) Photo:image];

CATransition *transition = [CATransition animation];
transition.type = kCATransitionReveal;
transition.duration = 0.5;
[review.layer addAnimation:transition forKey:nil];
[self.navigationController.view addSubview:review];

2.先給一個(gè)整個(gè)window的背景View,再加個(gè)展現(xiàn)照片的imageView。一般圖片都是劇中顯示,每個(gè)圖片都是帶有size屬性,可以根據(jù)圖片原有的size和屏幕款高比,從而寫(xiě)出展示的imageViewframe。

CGFloat scale = photo.size.width / self.frame.size.width;
CGFloat height = photo.size.height / scale;
    
self.backgroundColor = [UIColor blackColor];
    
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, (self.frame.size.height - height)/2, self.frame.size.width, height)];
imageView.image = photo;
imageView.userInteractionEnabled = YES;
[self addSubview:imageView];

3.接下來(lái)就是給imageView加手勢(shì)了,加上了各種手勢(shì)才能有各種交互的功能呀~

先加一個(gè)可以放大的功能思密達(dá)~~

UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchChangeScale:)];
[imageView addGestureRecognizer:pinchGesture];

- (void)pinchChangeScale:(UIPinchGestureRecognizer *)pinch {
    UIImageView *imageView = (UIImageView *)pinch.view;
    if (pinch.state == UIGestureRecognizerStateBegan) {
            lastScare = 1.0;
  }
    CGFloat scare = 1 - (lastScare - pinch.scale);
    CGAffineTransform currentTransform = imageView.transform;
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scare, scare);
    [imageView setTransform:newTransform];
    lastScare = pinch.scale;
}

當(dāng)然咯,要是能放大肯定是要能拖動(dòng)查看的說(shuō)~

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[imageView addGestureRecognizer:panGesture];

- (void)move:(UIPanGestureRecognizer *)pan {
    CGPoint transpoint = [pan translationInView:self];
    UIImageView *imageView = (UIImageView *)pan.view;
    if (pan.state == UIGestureRecognizerStateBegan) {
        firstX = imageView.center.x;
       firstY = imageView.center.y;
    }
    if (lastScare == 1.0f) {
        return;
    }
    transpoint = CGPointMake(firstX+transpoint.x, firstY+transpoint.y);
    [imageView setCenter:transpoint];
}

唔~還要有一個(gè)雙擊可以返回原來(lái)的狀態(tài)手勢(shì)!

UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapResolve:)];
    doubleTapGesture.numberOfTapsRequired = 2;
[imageView addGestureRecognizer:doubleTapGesture];

- (void)tapResolve:(UITapGestureRecognizer *)tap {
    UIImageView *imageView = (UIImageView *)tap.view;
    [imageView setTransform:transform];
    [imageView setFrame:imgFrame];
    lastScare = 1.0f;
}

但是還需要一個(gè)單擊返回的手勢(shì),雙擊和單擊會(huì)沖突,這個(gè)時(shí)候可以使用這個(gè)函數(shù)requireGestureRecognizerToFail。

最后如果是網(wǎng)上的圖片預(yù)覽,可能需要長(zhǎng)按本地保存的功能,可以這樣寫(xiě)喲:

- (void)longPress:(UILongPressGestureRecognizer *)longPress {
    UIImageView *imageView = (UIImageView *)longPress.view;
    if (longPress.state == UIGestureRecognizerStateBegan) {
        if (self.longpressblock && imageView.image) {
            self.longpressblock(imageView.image);
        }
    }
}

本地保存圖片的方法是這個(gè):UIImageWriteToSavedPhotosAlbum,之所以用block是因?yàn)檫@只是個(gè)view,沒(méi)辦法present出UIAlertController。

結(jié)束啦~

這就是一個(gè)簡(jiǎn)單的圖片預(yù)覽,demo的地址在這里:https://github.com/ioscick/-Demo
可以用于互相學(xué)習(xí)討論喲!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,030評(píng)論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,291評(píng)論 4 61
  • 遠(yuǎn)離了鄉(xiāng)村田野,從農(nóng)村走向城市,曾經(jīng)老一輩的夢(mèng)想,在我們這一代人的努力下,慢慢得以實(shí)現(xiàn),可是,當(dāng)城市生活的壓力越來(lái)...
    香之蘭兒閱讀 387評(píng)論 3 3
  • 1.記錄2016年讀過(guò)的有較大收獲的書(shū)籍,每本附以一句話點(diǎn)評(píng)。 《富爸爸窮爸爸》 富人和窮人的主要區(qū)別在于思想觀念...
    quiterr閱讀 158評(píng)論 0 0
  • 夜 靜人安 夜 里相念 想 起安好 你 在心間 夢(mèng) 在今夕 中 年相戀 吻 繞綿綿 我 愿永遠(yuǎn)
    獨(dú)白隨心閱讀 264評(píng)論 0 0

友情鏈接更多精彩內(nèi)容