CoreGraphices之UIGraphicsBeginIma

2018-01-08  本文已影响39人  MacPen

之前文章中使用UIGraphicsGetCurrentContext来获取上下文需要放在drawInRect中使用,在UIKit创建的时候,自动获取上下文。除了上面的通用的获取上下文的方法外,也可以使用UIGraphicsBeginImageContextUIGraphicsBeginPDFPageWithInfo来分别获取位图和PDF的上下文。它们的上下文获取的时候不需要通过UIKit,使用的时候要手动创建,使用完手动关闭。所以可以在需要的时候创建,没必要放在drawInRect方法中。使用UIGraphicsBeginImageContext可以做一些独特的功能,这里先简单的记其中三个最常用的。

1.给图片添加水印
2.屏幕截图并保存图片
3.图片裁剪

给图片添加图形和文字水印

- (UIImage *)getNewIconImgWithImage:(UIImage *)image {
    //1.获取规定大小的上下文,最好和图片的尺寸一致
    CGSize size = CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width*215/417);
    UIGraphicsBeginImageContext(size);
    //2.绘制图片
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    //3.绘制水印图片
    UIImage *icon = [UIImage imageNamed:@"icon120*120"];
    [icon drawInRect:CGRectMake(size.width-110, 15, 30, 30)];
    //4.绘制水印文字
    NSString *iconText = @"MacPen";
    NSDictionary *attributed = @{NSFontAttributeName:[UIFont fontWithName:@"Noteworthy" size:15],NSForegroundColorAttributeName:[UIColor orangeColor]};
    [iconText drawAtPoint:CGPointMake(size.width-70, 20) withAttributes:attributed];
    //5.获取绘制后的图片
    UIImage *iconImg = UIGraphicsGetImageFromCurrentImageContext();
    //6.关闭上下文
    UIGraphicsEndImageContext();
    return iconImg;
}
GraphicesView1.png

截取屏幕图片并将图片保存至系统相册

- (UIImage *)screenShotImage {
    //1.开启上下文
    UIGraphicsBeginImageContext(CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height));
    //2.获取上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    //3.截图
    [self.view.layer renderInContext:context];
    //4.获取新图片
    UIImage *screenImg = UIGraphicsGetImageFromCurrentImageContext();
    //5.关闭上下文
    UIGraphicsEndImageContext();
    //6.将图片保存至相册
    UIImageWriteToSavedPhotosAlbum(screenImg, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    return screenImg;
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    if (error == nil) {
        UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"提示" message:@"已保存至手机相册" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alertView animated:YES completion:nil];
        double delayInSeconds = 1.2;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            [alertView dismissViewControllerAnimated:YES completion:nil];
        });
    }else{
        UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"提示" message:@"保存失败" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alertView animated:YES completion:nil];
        double delayInSeconds = 1.2;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            [alertView dismissViewControllerAnimated:YES completion:nil];
        });
    }
}

GraphicesView2.png

裁剪图片

- (UIImage *)shapeImageWithImage:(UIImage *)preImage {
    //1、开启上下文
    CGSize size = CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width*215/417);
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    //2、设置裁剪区域
    UIBezierPath * path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(size.width/2-75, size.height/2-75, 150, 150)];
    [path addClip];
    //3、绘制图片
    [preImage drawAtPoint:CGPointZero];
    //4、获取新图片
    UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
    //5、关闭上下文
    UIGraphicsEndImageContext();
    //6、返回新图片
    return newImage;
}

GraphicesView3.png
参考链接:https://www.jianshu.com/p/3480818c6772
上一篇下一篇

猜你喜欢

热点阅读