- Home /
Question by
Mike-B · May 29, 2013 at 01:38 AM ·
iospluginobjective-c
Memory problem with UIImagePickerViewController
Hi,
This might be an iOS problem, but it is part of my unity plugin. I made a plugin to take a picture using the iOS image picker.
Once the picture has been captured, this method is called:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//Dismiss the picker
[[[UIApplication sharedApplication] keyWindow].rootViewController dismissModalViewControllerAnimated:NO];
[[UIApplication sharedApplication] keyWindow].rootViewController = _unityViewController;
//Create the 'photos' directory if needed
NSFileManager *fm = [NSFileManager defaultManager];
NSURL *tmpDirURL = [[[fm URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] objectAtIndex:0]
URLByAppendingPathComponent:@"photos"];
if (![fm fileExistsAtPath:tmpDirURL.path]) {
NSError *error;
[fm createDirectoryAtURL:tmpDirURL withIntermediateDirectories:NO attributes:nil error:&error];
if (error) {
UnitySendMessage("PhotoLoader", "LoadPhoto", [error.description UTF8String]);
return;
}
}
UIImage *im = [info valueForKey:UIImagePickerControllerOriginalImage];
//Save the file
NSURL *fileURL = [tmpDirURL URLByAppendingPathComponent:_filename];
NSData *data = UIImageJPEGRepresentation(im, 0.8f);
if([data writeToFile:fileURL.path atomically:YES])
{
UnitySendMessage("PhotoLoader", "LoadPhoto", [_filename UTF8String]);
}
else
{
UnitySendMessage("PhotoLoader", "LoadPhoto", "");
}
}
This works fine once, but the second time, my app crashes for running out of memory. I know the image picker is using a lot of memory, but when I cancel the picture (and therefore not run through the code above), the image picker is dismissed and all the memory is returned. So I think there is a leak somewhere in the method above... Can anyone help? thanks
Comment