源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

在iOS开发的Quartz2D使用中实现图片剪切和截屏功能

  • 时间:2021-04-18 21:39 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:在iOS开发的Quartz2D使用中实现图片剪切和截屏功能
[b]图片剪切 [/b]一、使用Quartz2D完成图片剪切 1.把图片显示在自定义的view中 先把图片绘制到view上。按照原始大小,把图片绘制到一个点上。 代码:
[u]复制代码[/u] 代码如下:
- (void)drawRect:(CGRect)rect {     UIImage *image2=[UIImage imageNamed:@"me"];     [image2 drawAtPoint:CGPointMake(100, 100)]; }
显示: [img]http://files.jb51.net/file_images/article/201512/201512192952332.png?201511193014[/img] 2.剪切图片让图片圆形展示 思路:先画一个圆,让图片显示在圆的内部,超出的部分不显示。 [img]http://files.jb51.net/file_images/article/201512/201512193045889.png?201511193116[/img] [img]http://files.jb51.net/file_images/article/201512/201512193123091.png?201511193131[/img] 注意:显示的范围只限于指定的剪切范围,无论往上下文中绘制什么东西,只要超出了这个范围的都不会显示。 代码:
[u]复制代码[/u] 代码如下:
- (void)drawRect:(CGRect)rect {     //画圆,以便以后指定可以显示图片的范围     //获取图形上下文     CGContextRef ctx=UIGraphicsGetCurrentContext();     CGContextAddEllipseInRect(ctx, CGRectMake(100, 100, 50, 50));         //指定上下文中可以显示内容的范围就是圆的范围     CGContextClip(ctx);     UIImage *image2=[UIImage imageNamed:@"me"];     [image2 drawAtPoint:CGPointMake(100, 100)]; }
显示: [img]http://files.jb51.net/file_images/article/201512/201512193142982.png?201511193150[/img] 3.剪切图片让图片三角形展示 代码:
[u]复制代码[/u] 代码如下:
- (void)drawRect:(CGRect)rect {     //画三角形,以便以后指定可以显示图片的范围     //获取图形上下文     CGContextRef ctx=UIGraphicsGetCurrentContext(); //    CGContextAddEllipseInRect(ctx, CGRectMake(100, 100, 50, 50));     CGContextMoveToPoint(ctx, 100, 100);     CGContextAddLineToPoint(ctx, 60, 150);      CGContextAddLineToPoint(ctx, 140, 150);     CGContextClosePath(ctx);             //注意:指定范围(也就是指定剪切的方法一定要在绘制范围之前进行调用)     //指定上下文中可以显示内容的范围就是圆的范围     CGContextClip(ctx);     UIImage *image2=[UIImage imageNamed:@"me"];     [image2 drawAtPoint:CGPointMake(100, 100)]; }
显示: [img]http://files.jb51.net/file_images/article/201512/201512193201697.png?201511193213[/img] [b]截屏 [/b]一、简单说明 在程序开发中,有时候需要截取屏幕上的某一块内容,比如捕鱼达人游戏。如图: [img]http://files.jb51.net/file_images/article/201512/201512193342760.png?201511193350[/img] 二、代码示例   storyboard界面搭建 [img]http://files.jb51.net/file_images/article/201512/201512193228398.png?201511193235[/img] 代码:
[u]复制代码[/u] 代码如下:
// //  YYViewController.m //  01-截屏 // //  Created by apple on 14-6-12. //  Copyright (c) 2014年 itcase. All rights reserved. // #import "YYViewController.h" #import "MBProgressHUD+NJ.h" @interface YYViewController () @property (weak, nonatomic) IBOutlet UIView *contentView; - (IBAction)BtnClick:(UIButton *)sender; @end
[u]复制代码[/u] 代码如下:
@implementation YYViewController - (void)viewDidLoad {     [super viewDidLoad]; } - (IBAction)BtnClick:(UIButton *)sender {         //延迟两秒保存     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{         //获取图形上下文         //    UIGraphicsBeginImageContext(self.view.frame.size);         UIGraphicsBeginImageContext(self.contentView.frame.size);         //将view绘制到图形上下文中                 //    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];         [self.contentView.layer renderInContext:UIGraphicsGetCurrentContext()];                      //将截屏保存到相册         UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext();                 UIImageWriteToSavedPhotosAlbum(newImage,self, @selector(image:didFinishSavingWithError:contextInfo:), nil);     }); }  - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {     if (error) {         [MBProgressHUD showError:@"保存失败,请检查是否拥有相关的权限"];     }else     { //        [MBProgressHUD showMessage:@"保存成功!"];         [MBProgressHUD showSuccess:@"保存成功!"];     } } @end
把截取的图片保存到手机的相册中: [img]http://files.jb51.net/file_images/article/201512/201512193247893.png?201511193254[/img] 说明:把整个屏幕画到一张图片里 1.创建一个bitmap的上下文 2.将屏幕绘制带上下文中 3.从上下文中取出绘制好的图片 4.保存图片到相册 补充:把图片写入到文件的代码
[u]复制代码[/u] 代码如下:
//3.从上下文中取出绘制好的图片      UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();           NSData *data = UIImagePNGRepresentation(newImage);           NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"abc.png"];      NSLog(@"%@", path);      [data writeToFile:path atomically:YES];
三、补充 保存成功和保存失败之后应该做些事情? 系统推荐的方法:
[u]复制代码[/u] 代码如下:
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {     if (error) {         [MBProgressHUD showError:@"保存失败,请检查是否拥有相关的权限"];     }else     { //        [MBProgressHUD showMessage:@"保存成功!"];         [MBProgressHUD showSuccess:@"保存成功!"];     } }
如果图片成功保存的话,那么就提示保存成功。 如果保存失败,那么提示失败 提示:保存失败常见有两个原因:1是内存不够,2是手机内部的权限不允许。 说明:如果当一个应用程序想要访问通讯录或相册,用户已经明确拒绝过,那么以后再要访问的话会直接拒绝。这个时候,可以提示用户去开启权限。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部