欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS實(shí)現(xiàn)點(diǎn)擊圖片放大和長(zhǎng)按保存圖片的示例

 更新時(shí)間:2018年03月06日 16:02:50   作者:FBY展菲  
本篇文章主要介紹了iOS實(shí)現(xiàn)點(diǎn)擊圖片放大和長(zhǎng)按保存圖片的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

一:簡(jiǎn)介

在項(xiàng)目中免不了會(huì)遇到,實(shí)名認(rèn)證上傳身份證、綁定銀行卡等功能。在實(shí)際操作中呢,會(huì)涉及到上傳圖片,在頁(yè)面布局時(shí),可能圖片不是一張,考慮到布局的美觀等因素,顯示圖片的位置變得很小,如果想查看上傳的圖片是否清晰,內(nèi)容是否完整,可能就需要放大才能實(shí)現(xiàn),下面就和大家分享一下我封裝的一類,完美的實(shí)現(xiàn)了圖片的縮放功能。

另外,這些博文都是來(lái)源于我日常開(kāi)發(fā)中的技術(shù)總結(jié),在時(shí)間允許的情況下,我會(huì)針對(duì)技術(shù)點(diǎn)分別分享iOS、Android兩個(gè)版本,盡量附上demo以供大家參考,如果有其他技術(shù)點(diǎn)需要,可在文章后留言,我會(huì)盡全力幫助大家。

二:實(shí)現(xiàn)思路分析

  1. 給UIImageView添加手勢(shì)
  2. 封裝一個(gè)繼承NSObject的FBYImageZoom類
  3. 寫(xiě)一個(gè)函數(shù)用來(lái)接收出入的UIImageView
  4. 根據(jù)傳入的UIImageView重新繪制在Window中
  5. 添加放大后背景視圖的顏色和透明度
  6. 使用動(dòng)畫(huà)放大展示ImageView
  7. 添加恢復(fù)ImageView原始尺寸的tap點(diǎn)擊事件
  8. 完成之后將背景視圖刪掉

三:實(shí)現(xiàn)源碼分析

根據(jù)實(shí)現(xiàn)思路分析,一步步進(jìn)行編碼實(shí)現(xiàn):

1. 給UIImageView添加手勢(shì)

self.myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 150, SCREEN_WIDTH-100, SCREEN_WIDTH-100)];
self.myImageView.image = [UIImage imageNamed:@"bankcard"];
//添加點(diǎn)擊事件
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scanBigImageClick:)];
[_myImageView addGestureRecognizer:tapGestureRecognizer];
[_myImageView setUserInteractionEnabled:YES];
[self.view addSubview:_myImageView];

2. 封裝一個(gè)繼承NSObject的FBYImageZoom類

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface FBYImageZoom : NSObject
@end

3. 寫(xiě)一個(gè)函數(shù)用來(lái)接收出入的UIImageView

/**
 * @param contentImageview 圖片所在的imageView
 */
+(void)ImageZoomWithImageView:(UIImageView *)contentImageview;

4. 根據(jù)傳入的UIImageView重新繪制在Window中

+(void)ImageZoomWithImageView:(UIImageView *)contentImageview{  
  UIWindow *window = [UIApplication sharedApplication].keyWindow;
  [self scanBigImageWithImage:contentImageview.image frame:[contentImageview convertRect:contentImageview.bounds toView:window]];
}

5. 添加放大后背景視圖的顏色和透明度

//當(dāng)前視圖
  UIWindow *window = [UIApplication sharedApplication].keyWindow;
  //背景
  UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
  [backgroundView setBackgroundColor:[UIColor colorWithRed:107/255.0 green:107/255.0 blue:99/255.0 alpha:0.6]];

6. 使用動(dòng)畫(huà)放大展示ImageView

  //動(dòng)畫(huà)放大所展示的ImageView
  [UIView animateWithDuration:0.4 animations:^{
    CGFloat y,width,height;
    y = ([UIScreen mainScreen].bounds.size.height - image.size.height * [UIScreen mainScreen].bounds.size.width / image.size.width) * 0.5;
    //寬度為屏幕寬度
    width = [UIScreen mainScreen].bounds.size.width;
    //高度 根據(jù)圖片寬高比設(shè)置
    height = image.size.height * [UIScreen mainScreen].bounds.size.width / image.size.width;
    [imageView setFrame:CGRectMake(0, y, width, height)];
    //重要! 將視圖顯示出來(lái)
    [backgroundView setAlpha:1];
  } completion:^(BOOL finished) {
    
  }];

7. 添加恢復(fù)ImageView原始尺寸的tap點(diǎn)擊事件

//添加點(diǎn)擊事件同樣是類方法 -> 作用是再次點(diǎn)擊回到初始大小
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideImageView:)];
[backgroundView addGestureRecognizer:tapGestureRecognizer];

/**
 * 恢復(fù)imageView原始尺寸
 */
+(void)hideImageView:(UITapGestureRecognizer *)tap{
  UIView *backgroundView = tap.view;
  //原始imageview
  UIImageView *imageView = [tap.view viewWithTag:1024];
  //恢復(fù)
  [UIView animateWithDuration:0.4 animations:^{
    [imageView setFrame:oldframe];
    [backgroundView setAlpha:0];
  } completion:^(BOOL finished) {
    [backgroundView removeFromSuperview];
  }];
}

8. 完成之后將背景視圖刪掉

//完成后操作->將背景視圖刪掉
[backgroundView removeFromSuperview];

四:項(xiàng)目實(shí)際使用

1. 引入封裝類FBYImageZoom

#import "FBYImageZoom.h"

2. 給UIImageView添加手勢(shì)

//添加點(diǎn)擊事件
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scanBigImageClick:)];

3. 調(diào)用封裝類函數(shù)

//瀏覽大圖點(diǎn)擊事件
-(void)scanBigImageClick:(UITapGestureRecognizer *)tap{
  NSLog(@"點(diǎn)擊圖片");
  UIImageView *clickedImageView = (UIImageView *)tap.view;
  [FBYImageZoom ImageZoomWithImageView:clickedImageView];
}

好了,到這里點(diǎn)擊圖片放大到全屏就完成了

4. 長(zhǎng)按保存圖片

另外就是實(shí)現(xiàn)長(zhǎng)按保存圖片的功能,這個(gè)功能很簡(jiǎn)單

首先增加長(zhǎng)按手勢(shì)

  //創(chuàng)建長(zhǎng)按手勢(shì)
  UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(imglongTapClick:)];
  //添加手勢(shì)
  [_myImageView addGestureRecognizer:longTap];

然后長(zhǎng)按手勢(shì)彈出警告視圖確認(rèn)

-(void)imglongTapClick:(UILongPressGestureRecognizer*)gesture
{  
  if(gesture.state==UIGestureRecognizerStateBegan)    
  {    
    UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"保存圖片" message:nil preferredStyle:UIAlertControllerStyleAlert];    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
      
      NSLog(@"取消保存圖片");
    }];
    
    UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"確認(rèn)" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
      
      NSLog(@"確認(rèn)保存圖片");      
      // 保存圖片到相冊(cè)
      UIImageWriteToSavedPhotosAlbum(self.myImageView.image,self,@selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:),nil);
      
    }];    
    [alertControl addAction:cancel];
    [alertControl addAction:confirm];    
    [self presentViewController:alertControl animated:YES completion:nil];
    
  }  
}

最后保存圖片后的回調(diào)

- (void)imageSavedToPhotosAlbum:(UIImage*)image didFinishSavingWithError: (NSError*)error contextInfo:(id)contextInfo

{
  NSString *message;  
  if(!error) {    
    message =@"成功保存到相冊(cè)";    
    UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
      
    }];
    [alertControl addAction:action];    
    [self presentViewController:alertControl animated:YES completion:nil];    
  }else
    
  {
    message = [error description];      
    UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
      
    }];    
    [alertControl addAction:action];    
    [self presentViewController:alertControl animated:YES completion:nil];    
  }  
}

到這里實(shí)現(xiàn)點(diǎn)擊圖片放大和長(zhǎng)按保存圖片功能就都是實(shí)現(xiàn)了,demo源碼已經(jīng)放在github上。

五:項(xiàng)目展示

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • iOS中 UIImage根據(jù)屏寬調(diào)整size的實(shí)例代碼

    iOS中 UIImage根據(jù)屏寬調(diào)整size的實(shí)例代碼

    最近做項(xiàng)目遇到這樣一個(gè)需求,要求UIImage根據(jù)屏幕寬度按照自己本身比例改變高度,下面通過(guò)本文給大家分享iOS UIImage根據(jù)屏寬調(diào)整size的實(shí)例代碼,需要的朋友參考下吧
    2017-01-01
  • 使用IOS AirPrint實(shí)現(xiàn)打印功能詳解

    使用IOS AirPrint實(shí)現(xiàn)打印功能詳解

    這篇文章主要介紹了使用IOS AirPrint實(shí)現(xiàn)打印功能詳解,想了解無(wú)線打印的同學(xué),一定要看一下
    2021-04-04
  • iOS實(shí)現(xiàn)一個(gè)可以在屏幕中自由移動(dòng)的按鈕

    iOS實(shí)現(xiàn)一個(gè)可以在屏幕中自由移動(dòng)的按鈕

    經(jīng)常在手機(jī)上看到可以隨意移動(dòng)的按鈕,正巧最近工作遇到了這個(gè)需求,索性就寫(xiě)一個(gè),下面這篇文章主要給大家介紹了利用iOS實(shí)現(xiàn)一個(gè)可以在屏幕中自由移動(dòng)的按鈕的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-07-07
  • iOS中使用UItableviewcell實(shí)現(xiàn)團(tuán)購(gòu)和微博界面的示例

    iOS中使用UItableviewcell實(shí)現(xiàn)團(tuán)購(gòu)和微博界面的示例

    這篇文章主要介紹了iOS中使用UItableviewcell實(shí)現(xiàn)團(tuán)購(gòu)和微博界面的示例,開(kāi)發(fā)語(yǔ)言基于傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2016-01-01
  • iOS App連續(xù)閃退時(shí)上報(bào)crash日志的方法詳解

    iOS App連續(xù)閃退時(shí)上報(bào)crash日志的方法詳解

    iOS App 有時(shí)可能遇到啟動(dòng)必 crash 的絕境:每次打開(kāi) App 都閃退,無(wú)法正常使用App。下面這篇文章主要給大家介紹了iOS App連續(xù)閃退時(shí)上報(bào)crash日志的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2018-04-04
  • iOS讓軟鍵盤(pán)消失的簡(jiǎn)單方法

    iOS讓軟鍵盤(pán)消失的簡(jiǎn)單方法

    一些文本輸入控件等待輸入時(shí)會(huì)彈出軟鍵盤(pán),我們可以設(shè)置這些控件的Did End On Exit之類的回調(diào)方法以在用戶點(diǎn)擊軟鍵盤(pán)上的done或return之列的按鍵時(shí)收起鍵盤(pán)
    2016-02-02
  • ios實(shí)現(xiàn)簡(jiǎn)單隨便移動(dòng)的AR功能

    ios實(shí)現(xiàn)簡(jiǎn)單隨便移動(dòng)的AR功能

    這篇文章主要為大家詳細(xì)介紹了ios實(shí)現(xiàn)簡(jiǎn)單隨便走的AR功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • Xcode8、iOS10升級(jí)問(wèn)題記錄

    Xcode8、iOS10升級(jí)問(wèn)題記錄

    本文給大家分享xcode8,ios10升級(jí)后的問(wèn)題記錄,可以幫大家到家更好的解決xcode,ios10升級(jí)遇到問(wèn)題,感興趣的朋友一起看看吧
    2016-09-09
  • IOS關(guān)閉鍵盤(pán)的方法

    IOS關(guān)閉鍵盤(pán)的方法

    在iOS應(yīng)用開(kāi)發(fā)中,有三類視圖對(duì)象會(huì)打開(kāi)虛擬鍵盤(pán),進(jìn)行輸入操作,但如何關(guān)閉虛擬鍵盤(pán),卻沒(méi)有提供自動(dòng)化的方法。這個(gè)需要我們自己去實(shí)現(xiàn)。
    2015-05-05
  • iOS輸入框(UITextField)密碼明暗文切換方法

    iOS輸入框(UITextField)密碼明暗文切換方法

    這篇文章主要介紹了iOS輸入框(UITextField)密碼明暗文的切換方法,代碼簡(jiǎn)短實(shí)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01

最新評(píng)論