最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS實現(xiàn)點擊圖片放大和長按保存圖片的示例

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

一:簡介

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

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

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

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

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

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

1. 給UIImageView添加手勢

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

2. 封裝一個繼承NSObject的FBYImageZoom類

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

3. 寫一個函數(shù)用來接收出入的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. 添加放大后背景視圖的顏色和透明度

//當前視圖
  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. 使用動畫放大展示ImageView

  //動畫放大所展示的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ù)圖片寬高比設置
    height = image.size.height * [UIScreen mainScreen].bounds.size.width / image.size.width;
    [imageView setFrame:CGRectMake(0, y, width, height)];
    //重要! 將視圖顯示出來
    [backgroundView setAlpha:1];
  } completion:^(BOOL finished) {
    
  }];

7. 添加恢復ImageView原始尺寸的tap點擊事件

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

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

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

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

四:項目實際使用

1. 引入封裝類FBYImageZoom

#import "FBYImageZoom.h"

2. 給UIImageView添加手勢

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

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

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

好了,到這里點擊圖片放大到全屏就完成了

4. 長按保存圖片

另外就是實現(xiàn)長按保存圖片的功能,這個功能很簡單

首先增加長按手勢

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

然后長按手勢彈出警告視圖確認

-(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:@"確認" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
      
      NSLog(@"確認保存圖片");      
      // 保存圖片到相冊
      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 =@"成功保存到相冊";    
    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];    
  }  
}

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

五:項目展示

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 解析iOS應用開發(fā)中對設計模式中的抽象工廠模式的實現(xiàn)

    解析iOS應用開發(fā)中對設計模式中的抽象工廠模式的實現(xiàn)

    這篇文章主要介紹了解析iOS應用開發(fā)中對設計模式中的抽象工廠模式的實現(xiàn),示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2016-03-03
  • 簡介iOS開發(fā)中應用SQLite的模糊查詢和常用函數(shù)

    簡介iOS開發(fā)中應用SQLite的模糊查詢和常用函數(shù)

    這篇文章主要介紹了iOS開發(fā)中應用SQLite的模糊查詢和常用函數(shù),SQLite是一個可作嵌入式的數(shù)據(jù)庫非常適合小型應用使用,需要的朋友可以參考下
    2015-12-12
  • 解決Xcode8打包上傳構建版本無效的辦法

    解決Xcode8打包上傳構建版本無效的辦法

    這篇文章主要介紹的是自己在打包上傳項目的時候遇到的一個問題,通過自己的努力一步步解決了,現(xiàn)將解決方法方法分享給大家,希望給同樣遇到這個問題的朋友們能有所幫助,下面來一起看看吧。
    2016-09-09
  • iOS擼一個簡單路由Router的實現(xiàn)代碼

    iOS擼一個簡單路由Router的實現(xiàn)代碼

    這篇文章主要介紹了iOS擼一個簡單路由Router的實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-09-09
  • ios實現(xiàn)簡單隨便移動的AR功能

    ios實現(xiàn)簡單隨便移動的AR功能

    這篇文章主要為大家詳細介紹了ios實現(xiàn)簡單隨便走的AR功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • iOS自定義日期選擇器

    iOS自定義日期選擇器

    這篇文章主要為大家詳細介紹了iOS自定義日期選擇器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • iOS中常用設置返回按鈕

    iOS中常用設置返回按鈕

    本文給大家分享一段代碼關于iOS中常用設置返回按鈕的實現(xiàn)方法,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友參考下
    2016-12-12
  • 總結IOS中隱藏軟鍵盤的三種方式

    總結IOS中隱藏軟鍵盤的三種方式

    在IOS開發(fā)中,軟鍵盤是開發(fā)者們經(jīng)常需要打交道的地方,下面為大家?guī)砦艺砜偨Y的三種隱藏鍵盤的方法。有需要的可以參考借鑒。
    2016-08-08
  • iOS中UIActivityIndicatorView的用法及齒輪等待動畫實例

    iOS中UIActivityIndicatorView的用法及齒輪等待動畫實例

    UIActivityIndicatorView活動指示器最常見的用法便是用來制作那個程序中的齒輪轉動的等待效果,接下來我們回來簡單整理iOS中UIActivityIndicatorView的用法及齒輪等待動畫實例:
    2016-05-05
  • IOS實現(xiàn)圓形圖片效果的兩種方法

    IOS實現(xiàn)圓形圖片效果的兩種方法

    這篇文章介紹在IOS中如何實現(xiàn)圓形圖片,實現(xiàn)后的效果很贊,有需要的可以參考借鑒。
    2016-08-08

最新評論

都匀市| 顺昌县| 彝良县| 贡觉县| 万州区| 临朐县| 德惠市| 金昌市| 谷城县| 哈尔滨市| 华容县| 开化县| 甘孜县| 昌吉市| 化隆| 元氏县| 永吉县| 抚松县| 吉林市| 河北省| 揭阳市| 洞口县| 合川市| 临沭县| 丽江市| 潢川县| 吴桥县| 萝北县| 娱乐| 林芝县| 潮州市| 建始县| 太原市| 台江县| 垦利县| 银川市| 昌邑市| 娄底市| 永寿县| 广汉市| 东兴市|