如何放大和裁剪 UIImage?
这是我的代码,但它崩溃了......有什么想法吗?
UIImage *tempImage = [[UIImage alloc] initWithData:imageData];
CGImageRef imgRef = [tempImage CGImage];
[tempImage release];
CGFloat width = CGImageGetWidth(imgRef);
CGFloat height = CGImageGetHeight(imgRef);
CGRect bounds = CGRectMake(0, 0, width, height);
CGSize size = bounds.size;
CGAffineTransform transform = CGAffineTransformMakeScale(4.0, 4.0);
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextConcatCTM(context, transform);
CGContextDrawImage(context, bounds, imgRef);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
我在这里缺少什么?基本上只是尝试放大图像并将其裁剪为与原始大小相同。
谢谢
Here's the code I have but it's crashing ... any ideas?
UIImage *tempImage = [[UIImage alloc] initWithData:imageData];
CGImageRef imgRef = [tempImage CGImage];
[tempImage release];
CGFloat width = CGImageGetWidth(imgRef);
CGFloat height = CGImageGetHeight(imgRef);
CGRect bounds = CGRectMake(0, 0, width, height);
CGSize size = bounds.size;
CGAffineTransform transform = CGAffineTransformMakeScale(4.0, 4.0);
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextConcatCTM(context, transform);
CGContextDrawImage(context, bounds, imgRef);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
What am I missing here? Basically just trying to scale image up and crop it to be same size as original.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是这一行:
或者更准确地说,是这一行的直接后续内容:
您在这里获取一个 CF 对象,
CGImageRef
。 Core Foundation 对象只有保留/释放内存管理,但没有自动释放对象。因此,当您释放第二行中的UIImage
时,CGImageRef
也会被删除。这再次意味着当您尝试将其绘制到那里时,它是未定义的。我可以想到三个修复方法:
[tempImage autorelease];
CFRetain< 释放图像/code> 和
CFRelease
。The problem is this line:
Or more precise, the direct follow-up of this line:
You are getting a CF object here, the
CGImageRef
. Core Foundation object only have the retain/release memory management, but no autoreleased objects. Hence, when you release theUIImage
in the second row, theCGImageRef
will be deleted as well. And this again means that it's undefined when you try to draw it down there.I can think of three fixes:
[tempImage autorelease];
CFRetain
andCFRelease
.试试这个:
只要你想捕获屏幕,就使用下面的行
Try this one:
use the below line whenever you want to capture the screen