iOS进阶之旅-图片裁剪

来源:互联网 发布:js加载阻塞页面渲染 编辑:程序博客网 时间:2024/06/16 10:58

关于

UIGraphicsBeginImageContext(CGSize size);

UIGraphicsBeginImageContextWithOptions(CGSize size,BOOL opaque, CGFloat scale);

UIGraphicsBeginImageContext开启指定区域的图形上下文

UIGraphicsBeginImageContextWithOptions在前者的基础上添加opaque(是否透明),scale(缩放比例)

我们知道,iOS的图形显示是基于view的layer的,而图形上下文我们可以理解为一个针对layer的画布,我们截图的步骤就是,拿到画布(图形上下文),绘制图片,拿到图片,关闭上下文。

1、绘制一个圆,截取图片然后将其存入沙盒

    // 开启一个 200 * 200 的图片的绘图上下文

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(200,200), NO, 0);  

    // 获取当前上下文

    CGContextRef ctx =UIGraphicsGetCurrentContext();

    // 画圆

    CGContextAddArc(ctx, 100, 100, 80,0, 2 * M_PI, 1);

    // 渲染

    CGContextStrokePath(ctx);

    // 通过当前的图片的图形上下文获取 图片

    UIImage* image =UIGraphicsGetImageFromCurrentImageContext();

    // 关闭图片的图形上下文

    UIGraphicsEndImageContext();

    // 保存到沙盒当中

    NSString* path =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)[0];

    path = [path stringByAppendingPathComponent:@"xx.png"];

    // image转化成 NSData类型

    NSData* data =UIImagePNGRepresentation(image);

    // 然后在通过data对象  write to file 来写入到沙盒中

    // atomically 线程安全的

    [data writeToFile:pathatomically:YES];

2、裁剪圆形图片,存入相册

   // -1.获取image对象

    UIImage* image = [UIImageimageNamed:@"booskai"];


    // -2.开启图片的图形上下文

    UIGraphicsBeginImageContextWithOptions(image.size,NO, 0);


    // -3.获取图片的图形上下文

    CGContextRef ctx =UIGraphicsGetCurrentContext();


    // -4.在上下文中绘制裁剪的形状

    CGContextAddArc(ctx, image.size.width *0.5, image.size.height *0.5, image.size.width *0.5, 0, 2 * M_PI, 1);


    // -5.裁剪

    CGContextClip(ctx);

    

    // -6.在裁剪过后的上下文中画图

    [image drawAtPoint:CGPointZero];

    

    // -7.通过图片的图形上下文获取图片

    UIImage* newImage =UIGraphicsGetImageFromCurrentImageContext();


    // -8.关闭图形上下文

    UIGraphicsEndImageContext();


    // 保存到相册,不带回调

    //    UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil);


    // @selector 不能随便写 使用注释当中的方法,带回调

    UIImageWriteToSavedPhotosAlbum(newImage, self, @selector(image:didFinishSavingWithError:contextInfo:),@"hello1111");

       

       NSData * data = UIImagePNGRepresentation(newImage);

       NSString * path =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)[0];

       path = [path stringByAppendingPathComponent:@"xx.png"];

       

       [data writeToFile:path atomically:YES];


//回调方法

- (void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo

{

    NSLog(@"save ok!!! %@-- %@", contextInfo, error);

}


3、裁剪带圆环的图片

 //画一个带圆环的图片


    // 拿到图片

    UIImage* image = [UIImageimageNamed:@"booskai"];


    // margin

    CGFloat margin = 10;


    // 拿到上下文的size

    CGSize ctxSize = CGSizeMake(image.size.width +2 * margin, image.size.height +2 * margin);


    // 获取图片的上下文

    UIGraphicsBeginImageContextWithOptions(ctxSize,NO, 0);


    // 拿到当前的上下文

    CGContextRef ctx =UIGraphicsGetCurrentContext();


    // 圆心

    CGPoint arcCenter = CGPointMake(ctxSize.width * 0.5, ctxSize.height * 0.5);


    // 半径

    CGFloat radius = (ctxSize.width - margin) *0.5;


    // 画圆

    CGContextAddArc(ctx, arcCenter.x, arcCenter.y, radius,0, 2 * M_PI, 1);


    // 设置线宽

    CGContextSetLineWidth(ctx, margin);


    // 渲染

    CGContextStrokePath(ctx);


    // 画圆

    CGContextAddArc(ctx, arcCenter.x, arcCenter.y, image.size.width *0.5, 0, 2 * M_PI, 1);


    // 裁剪

    CGContextClip(ctx);


    // 画图片

    [image drawAtPoint:CGPointMake(margin, margin)];


    // 拿图

    UIImage* newImage =UIGraphicsGetImageFromCurrentImageContext();


    // 关闭图片上下文

    UIGraphicsEndImageContext();


    // 保存

    UIImageWriteToSavedPhotosAlbum(newImage,nil, nil, nil);


4、屏幕截图

    //开启上下文

    UIGraphicsBeginImageContextWithOptions(self.view.frame.size,NO, 0);

    // 获取上下文

    CGContextRef ctx =UIGraphicsGetCurrentContext();

    // 对控制器的view进行操作

    [self.view.layerrenderInContext:ctx];

    // 获取到 截图

    UIImage* image =UIGraphicsGetImageFromCurrentImageContext();


    // 关闭上下文

    UIGraphicsEndImageContext();


    // 保存到相册

    UIImageWriteToSavedPhotosAlbum(image,nil, nil, nil);





1 0