我正在尝试使用UIImagePickerController创建我从照片库或相机导入的图像缩略图 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)selectedImage editingInfo:(NSDictionary * )editingInfo 当我使用相机时,点击图像的缩略图很容易创建,但是当通过从照片库中选择图像来完成同样的操作我的应用程序崩溃..当我在我的应用程序中启用NSZombie时显示 f- [UIImage drawInRect:]:在myThumbnail变量上发送到解除分配的实例0x5abc140 的消息..
我可能做错了什么?
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)selectedImage editingInfo:(NSDictionary *)editingInfo
{
[self saveImage:selectedImage];
}
- (void)saveImage:(UIImage *)img
{
myThumbNail = img;
UIGraphicsBeginImageContext(CGSizeMake(60.0,60.0));
[myThumbNail drawInRect:CGRectMake(0.0, 0.0, 60.0, 60.0)];// gets killed here
}
答案 0 :(得分:3)
saveImage:method中的图片是什么?我认为应该是img。 希望这有帮助
- (void)saveImage:(UIImage *)img
{
[img retain];
//alloc your myThumbNail before this
myThumbNail = img;
[img release];
UIGraphicsBeginImageContext(CGSizeMake(60.0,60.0));
[myThumbNail drawInRect:CGRectMake(0.0, 0.0, 60.0, 60.0)];// gets killed here
}
答案 1 :(得分:1)
+ (UIImage *)resizeImage:(UIImage *)image toResolution:(int)resolution
{
NSData *imageData = UIImagePNGRepresentation(image);
CGImageSourceRef src = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);
CFDictionaryRef options = (__bridge CFDictionaryRef) @{
(id) kCGImageSourceCreateThumbnailWithTransform : @YES,
(id) kCGImageSourceCreateThumbnailFromImageAlways : @YES,
(id) kCGImageSourceThumbnailMaxPixelSize : @(resolution)
};
CGImageRef thumbnail = CGImageSourceCreateThumbnailAtIndex(src, 0, options);
CFRelease(src);
UIImage *img = [[UIImage alloc]initWithCGImage:thumbnail];
return img;
}
此方法将UIImage和分辨率作为参数,并返回具有所需分辨率的UIImage。
例如:如果要将1024x1024分辨率图像转换为240x240,则在argument1中传递1024x1024 UIImage,在参数2中传递240。作为回报,您将获得240x240 UIImage(或任何您想要的缩略图大小)。
答案 2 :(得分:0)
您是否在myThumbNail上调用alloc init?听起来你要么首先没有为它分配内存,要么你已经将它释放到了你不应该拥有的地方。