Reputation: 1853
i am trying to create thumbnail of a image i am importing from photo library or camera using a UIImagePickerController using - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)selectedImage editingInfo:(NSDictionary *)editingInfo when i use the camera the thumbnail of the image clicked is easily created but when the same is done by selecting a image from photo library my app crashes.. When i enabled NSZombie in my app it shows f-[UIImage drawInRect:]: message sent to deallocated instance 0x5abc140 on myThumbnail variable..
What am i possible doing wrong??
- (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
}
Upvotes: 1
Views: 5253
Reputation: 1486
+ (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;
}
This method takes a UIImage and the resolution as the arguments and returns a UIImage with the required resolution.
For example: If you want to convert 1024x1024 resolution image to 240x240, then pass 1024x1024 UIImage in argument1 and 240 in argument2. In return you get 240x240 UIImage (or whatever thumbnail size you want).
Upvotes: 1
Reputation: 26400
What is image in your saveImage: method? I think it should be img. Hope this helps
- (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
}
Upvotes: 3
Reputation: 5122
Are you calling alloc init on myThumbNail anywhere? It sounds like you either have not allocated memory for it in the first place, or you have released it somewhere you shouldn't have.
Upvotes: 0