[英]How to scale up and crop a 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();
我在这里想念什么? 基本上只是尝试按比例放大图像并将其裁剪为与原始图像相同的大小。
谢谢
问题是这一行:
CGImageRef imgRef = [tempImage CGImage];
更确切地说,这条线的直接后续行动:
[tempImage release];
您将在此处获得CF对象CGImageRef
。 Core Foundation对象仅具有保留/释放内存管理,而没有自动释放的对象。 因此,当您在第二行中释放UIImage
时, CGImageRef
也将被删除。 这又意味着当您尝试将其拖到此处时,它是未定义的。
我可以想到三个解决方法:
[tempImage autorelease];
CFRetain
和CFRelease
保留并释放图像。 试试这个:
-(CGImageRef)imageCapture
{
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect rect= CGRectMake(0,0 ,320, 480);
CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], rect);
return imageRef;
}
每当您要捕获屏幕时,请使用以下行
UIImage *captureImg=[[UIImage alloc] initWithCGImage:[self imageCapture]];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.