亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

IOS實(shí)現(xiàn)手動(dòng)截圖并保存

 更新時(shí)間:2016年01月10日 15:01:21   作者:世俗孤島  
這篇文章主要介紹了IOS實(shí)現(xiàn)手動(dòng)剪裁圖片并保存到相冊(cè),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例介紹了iOS手動(dòng)剪裁圖片并保存到相冊(cè)的詳細(xì)代碼,分享給大家供大家參考,具體內(nèi)容如下

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

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

2、效果圖

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

  • 點(diǎn)擊“是”,保存圖片
  • 點(diǎn)擊“否”,暫時(shí)什么都不做

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

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

2、設(shè)置通過手勢(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上,此時(shí)的clipView不會(huì)顯示(未設(shè)置其frame)
    [self.view addSubview:_clipView];
  }
  return _clipView;
}

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

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

2)、增加成員屬性,記錄pan手勢(shì)開始的點(diǎn)

@property (nonatomic, assign) CGPoint startPoint;

3)、監(jiān)聽手勢(shì)的移動(dòng)

- (void)pan:(UIPanGestureRecognizer *)pan
{
  CGPoint endPoint = CGPointZero;

  if (pan.state == UIGestureRecognizerStateBegan)
  {
    /**開始點(diǎn)擊時(shí),記錄手勢(shì)的起點(diǎn)**/
    self.startPoint = [pan locationInView:self.view];
  }
  else if(pan.state == UIGestureRecognizerStateChanged)
  {
    /**當(dāng)手勢(shì)移動(dòng)時(shí),動(dòng)態(tài)改變終點(diǎn)的值,并計(jì)算起點(diǎn)與終點(diǎn)之間的矩形區(qū)域**/
    endPoint = [pan locationInView:self.view];
    //計(jì)算矩形區(qū)域的寬高
    CGFloat w = endPoint.x - self.startPoint.x;
    CGFloat h = endPoint.y - self.startPoint.y;
    //計(jì)算矩形區(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)
  {
    /**若手勢(shì)停止,將剪切區(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提示用戶,是否將圖片保存至相冊(cè)
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"保存圖片" message:@"是否將圖片保存至相冊(cè)?" delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];
    [alertView show];
}
}

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

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

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。

相關(guān)文章

最新評(píng)論