OC,oc語言

來源:互聯網
上載者:User

OC,oc語言
實現效果

  • 操作步驟

    • 繪製一個矩形框,彈出一個alertView,提示是否儲存圖片
    • 點擊"是",將圖片儲存到相簿
    • 在相簿中查看儲存的圖片
實現思路
  • 在控制器的view上添加一個imageView,設定圖片
  • 在控制器的view上添加一個pan手勢
  • 跟蹤pan手勢,繪製一個矩形框(圖片的剪下地區
  • 在pan手勢結束時,通過alertView提示“是否將圖片儲存至相簿?”

    • 點擊“是”,儲存圖片
    • 點擊“否”,暫時什麼都不做
實現步驟
  • 通過storyboard在控制器的view上添加一個imageView(設定圖片),並在控制器的.m檔案中擁有該屬性

    @property (weak, nonatomic) IBOutlet UIImageView *imageView;
  • 設定通過手勢繪製的圖片的剪下地區

    • 將圖片的剪下地區作為成員屬性clipView
    @property (nonatomic, weak) UIView *clipView;
    • 通過懶載入的方式建立clipView,並初始化
    - (UIView *)clipView{//如果clipView為被建立,就建立if (_clipView == nil){    UIView *view = [[UIView alloc] init];    _clipView = view;    //設定clipView的背景色和透明度    view.backgroundColor = [UIColor blackColor];    view.alpha = 0.5;    //將clipView添加到控制器的view上,此時的clipView不會顯示(未設定其frame)    [self.view addSubview:_clipView];}return _clipView;}
  • 給控制器的view添加pan手勢,跟蹤pan手勢,繪製圖片剪下地區

    • 建立並添加手勢
    /**建立手勢**/UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];/***每當pan手勢的位置發生變化,就會調用pan:方法,並將手勢作為參數傳遞*//**添加手勢**/[self.view addGestureRecognizer:pan];
    • 增加成員屬性,記錄pan手勢開始的點
    @property (nonatomic, assign) CGPoint startPoint;
    • 監聽手勢的移動
    - (void)pan:(UIPanGestureRecognizer *)pan{CGPoint endPoint = CGPointZero;if (pan.state == UIGestureRecognizerStateBegan){/**開始點擊時,記錄手勢的起點**/    self.startPoint = [pan locationInView:self.view];}else if(pan.state == UIGestureRecognizerStateChanged){/**當手勢移動時,動態改變終點的值,並計算起點與終點之間的矩形地區**/    endPoint = [pan locationInView:self.view];    //計算矩形地區的寬高    CGFloat w = endPoint.x - self.startPoint.x;    CGFloat h = endPoint.y - self.startPoint.y;    //計算矩形地區的frame    CGRect clipRect = CGRectMake(self.startPoint.x, self.startPoint.y, w, h);    //設定剪下地區的frame    self.clipView.frame = clipRect;}else if(pan.state == UIGestureRecognizerStateEnded){/**若手勢停止,將剪下地區的圖片內容繪製到圖形上下文中**///開啟位元影像上下文    UIGraphicsBeginImageContextWithOptions(self.imageView.bounds.size, NO, 0);    //建立大小等於剪下地區大小的封閉路徑    UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.clipView.frame];    //設定超出的內容不顯示,    [path addClip];    //擷取繪圖上下文    CGContextRef context = UIGraphicsGetCurrentContext();    //將圖片渲染的上下文中    [self.imageView.layer renderInContext:context];    //擷取上下文中的圖片    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();    //關閉位元影像上下文    UIGraphicsEndImageContext();    //移除剪下地區視圖控制項,並清空    [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];}}
  • 設定alertView的代理方法,確定是否儲存圖片

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

     

  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.