国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

IOS如何實現(xiàn)手動截圖并保到相冊

2020-02-19 15:56:35
字體:
供稿:網(wǎng)友

本篇文章介紹了IOS如何實現(xiàn)手動截圖并保到相冊,下面我們就來看看詳細(xì)的教程,需要的朋友可以參考下。

一、實現(xiàn)效果
1、操作步驟

  • 繪制一個矩形框,彈出一個alertView,提示是否保存圖片
  • 點擊"是",將圖片保存到相冊
  • 在相冊中查看保存的圖片

2、效果圖

二、實現(xiàn)思路
1、在控制器的view上添加一個imageView,設(shè)置圖片
2、在控制器的view上添加一個pan手勢
3、跟蹤pan手勢,繪制一個矩形框(圖片的剪切區(qū)域)
4、在pan手勢結(jié)束時,通過alertView提示“是否將圖片保存至相冊?”

  • 點擊“是”,保存圖片
  • 點擊“否”,暫時什么都不做

三、實現(xiàn)步驟
1、通過storyboard在控制器的view上添加一個imageView(設(shè)置圖片),并在控制器的.m文件中擁有該屬性

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

2、設(shè)置通過手勢繪制的圖片的剪切區(qū)域
將圖片的剪切區(qū)域作為成員屬性clipView

@property (nonatomic, weak) UIView *clipView;

3、通過懶加載的方式創(chuàng)建clipView,并初始化

- (UIView *)clipView{  //如果clipView為被創(chuàng)建,就創(chuàng)建  if (_clipView == nil)  {    UIView *view = [[UIView alloc] init];    _clipView = view;    //設(shè)置clipView的背景色和透明度    view.backgroundColor = [UIColor blackColor];    view.alpha = 0.5;    //將clipView添加到控制器的view上,此時的clipView不會顯示(未設(shè)置其frame)    [self.view addSubview:_clipView];  }  return _clipView;}

4、給控制器的view添加pan手勢,跟蹤pan手勢,繪制圖片剪切區(qū)域
1)、創(chuàng)建并添加手勢

/**創(chuàng)建手勢**/UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];  /**  *每當(dāng)pan手勢的位置發(fā)生變化,就會調(diào)用pan:方法,并將手勢作為參數(shù)傳遞  *//**添加手勢**/[self.view addGestureRecognizer:pan];

2)、增加成員屬性,記錄pan手勢開始的點

@property (nonatomic, assign) CGPoint startPoint;

3)、監(jiān)聽手勢的移動

- (void)pan:(UIPanGestureRecognizer *)pan{  CGPoint endPoint = CGPointZero;  if (pan.state == UIGestureRecognizerStateBegan)  {    /**開始點擊時,記錄手勢的起點**/    self.startPoint = [pan locationInView:self.view];  }  else if(pan.state == UIGestureRecognizerStateChanged)  {    /**當(dāng)手勢移動時,動態(tài)改變終點的值,并計算起點與終點之間的矩形區(qū)域**/    endPoint = [pan locationInView:self.view];    //計算矩形區(qū)域的寬高    CGFloat w = endPoint.x - self.startPoint.x;    CGFloat h = endPoint.y - self.startPoint.y;    //計算矩形區(qū)域的frame    CGRect clipRect = CGRectMake(self.startPoint.x, self.startPoint.y, w, h);    //設(shè)置剪切區(qū)域的frame    self.clipView.frame = clipRect;  }  else if(pan.state == UIGestureRecognizerStateEnded)  {    /**若手勢停止,將剪切區(qū)域的圖片內(nèi)容繪制到圖形上下文中**/    //開啟位圖上下文    UIGraphicsBeginImageContextWithOptions(self.imageView.bounds.size, NO, 0);    //創(chuàng)建大小等于剪切區(qū)域大小的封閉路徑    UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.clipView.frame];    //設(shè)置超出的內(nèi)容不顯示,    [path addClip];    //獲取繪圖上下文    CGContextRef context = UIGraphicsGetCurrentContext();    //將圖片渲染的上下文中    [self.imageView.layer renderInContext:context];    //獲取上下文中的圖片    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();    //關(guān)閉位圖上下文    UIGraphicsEndImageContext();    //移除剪切區(qū)域視圖控件,并清空    [self.clipView removeFromSuperview];    self.clipView = nil;    //將圖片顯示到imageView上    self.imageView.image = image;    //通過alertView提示用戶,是否將圖片保存至相冊    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"保存圖片" message:@"是否將圖片保存至相冊?" delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];    [alertView show];}}

4)、設(shè)置alertView的代理方法,確定是否保存圖片

- (void)alertView:(nonnull UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{  //若點擊了“是”,則保存圖片  if (buttonIndex == 1)  {    UIImageWriteToSavedPhotosAlbum(self.imageView.image, nil, nil, nil);    /**    * 該方法可以設(shè)置保存完畢調(diào)用的方法,此處未進行設(shè)置    */  }}

以上就是IOS如何實現(xiàn)手動截圖并保到相冊的全部內(nèi)容介紹了,希望小編整理的相關(guān)知識和資料都對你們有所幫助,更多內(nèi)容請繼續(xù)關(guān)注武林技術(shù)頻道網(wǎng)站!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 固原市| 广西| 武清区| 视频| 边坝县| 温州市| 桐庐县| 崇阳县| 华池县| 宁津县| 万盛区| 峨边| 胶南市| 尉犁县| 宜章县| 固原市| 陆丰市| 江口县| 万荣县| 陵水| 黔西| 永泰县| 明星| 葵青区| 六盘水市| 芜湖县| 晴隆县| 军事| 新郑市| 大宁县| 同心县| 大荔县| 绿春县| 古交市| 武威市| 永康市| 阳信县| 丽江市| 白城市| 麻栗坡县| 稷山县|