使用核心图形和图层蒙版裁剪 UIImage [英] Crop UIImage with Core Graphics and Layer Mask

查看:27
本文介绍了使用核心图形和图层蒙版裁剪 UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在关注这个 SO 问题来裁剪一个与 IBOutlet 连接的 UIImageView 和一个图层蒙版,这是我关注的问题和答案 如何在 iOS 中的 UIImageView 中裁剪圆圈内的图像.但是现在我想知道如何在不向用户显示 UIImageView 的情况下完成此操作.(所以一个没有连接到IBOutlet).我对帖子发表了评论,用户说使用核心图形裁剪图像而无需连接.我不知道该怎么做?所以我想知道如何将裁剪应用于具有核心核心图形的 UIImage 或 UIImageView?

提前感谢您的帮助.

解决方案

例如,您可以执行以下操作,创建位图上下文,添加剪切路径,将图像绘制到该上下文中,然后提取脱离上下文的图像:

NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"jpg"];UIImage *image = [UIImage imageWithContentsOfFile:path];size_t width = image.size.width, height = image.size.height;CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();CGBitmapInfo bitmapInfo = (CGBitmapInfo)kCGImageAlphaPremultipliedLast;CGContextRef context = CGBitmapContextCreate(NULL, width, height, 8, 4 * width, colorSpace, bitmapInfo);CGRect 矩形 = ...CGContextAddEllipseInRect(context, rect);CGContextClip(上下文);CGContextDrawImage(context, CGRectMake(0, 0, width, height), image.CGImage);CGImageRef imageRef = CGBitmapContextCreateImage(context);UIImage *final = [UIImage imageWithCGImage:imageRef];NSData *data = UIImagePNGRepresentation(final);NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];NSString *savePath = [documentsPath stringByAppendingPathComponent:@"final.png"];[data writeToFile:savePath atomically:YES];CGColorSpaceRelease(colorSpace);CGImageRelease(imageRef);CGContextRelease(上下文);

有关更多信息,请参阅Quartz 2D 编程指南.或参考 CGContext 参考.>

I have been following this SO question to crop an UIImageView that is connected with IBOutlet with a Layer Mask, here is the question and answer I was following How to crop image inside the circle in UIImageView in iOS. But now I am wondering how this could be done without have a UIImageView shown to the user. (So one that is not connected to IBOutlet). I commented on the post and the user said to use core graphics to crop the image without having it connected. Im not sure how to do this? So I am wondering how to apply a crop to a UIImage or UIImageView with core Core Graphics?

Thanks for the help in advance.

解决方案

You could, for example, do something like the following, which creates a bitmap context, adds a clipping path, draws an image into that context, and then extracts the image back out of that context:

NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"jpg"];
UIImage *image = [UIImage imageWithContentsOfFile:path];

size_t width = image.size.width, height = image.size.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = (CGBitmapInfo)kCGImageAlphaPremultipliedLast;
CGContextRef context = CGBitmapContextCreate(NULL, width, height, 8, 4 * width, colorSpace, bitmapInfo);

CGRect rect = ...
CGContextAddEllipseInRect(context, rect);
CGContextClip(context);

CGContextDrawImage(context, CGRectMake(0, 0, width, height), image.CGImage);

CGImageRef imageRef = CGBitmapContextCreateImage(context);
UIImage *final = [UIImage imageWithCGImage:imageRef];

NSData *data = UIImagePNGRepresentation(final);

NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *savePath      = [documentsPath stringByAppendingPathComponent:@"final.png"];

[data writeToFile:savePath atomically:YES];

CGColorSpaceRelease(colorSpace);
CGImageRelease(imageRef);
CGContextRelease(context);

For more information, see the Quartz 2D Programming Guide. Or refer to the CGContext Reference.

这篇关于使用核心图形和图层蒙版裁剪 UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆