iPhone: Not getting image path URL in didFinishPickingImage
I'm trying to get image path after picked from photo album using the below code. But it always gives image path as NULL/crash. What is the reason? Could someone help me?
-(IBAction) LaunchAlbum :(id) sender
{
// Updated - overlay code.
MyAppDelegate *appDelegate = (MyAppDelegate *)[UIApplication sharedApplication].delegate;
appDelegate.pickerController = [[UIImagePickerController alloc] init];
appDelegate.pickerController.delegate = self;
//appDelegate.pickerController.allowsEditing = NO;
//appDelegate.pickerController.mediaTypes 开发者_JAVA技巧= [NSArray arrayWithObject:@"public.image"];
appDelegate.pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:appDelegate.pickerController animated:YES];
[ appDelegate.pickerController release];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
imageUrl = [info valueForKey:@"UIImagePickerControllerReferenceURL"]; // CRASH
selectedImage = [imageUrl path];
NSLog(@"selectedImage: %@", selectedImage);
}
You want:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSURL* imageUrl = [info valueForKey:UIImagePickerControllerMediaURL];
selectedImage = [imageUrl path];
NSLog(@"selectedImage: %@", selectedImage);
}
There were several problems with what you were trying:
imagePickerController:didFinishPickingImage:editingInfo:
is deprecated- Keys like
UIImagePickerControllerMediaURL
are constants that should be used as is, not quoted as strings. - You probably want
UIImagePickerControllerMediaURL
instead ofUIImagePickerControllerReferenceURL
From UIImagePickerController class reference:
editingInfo
A dictionary containing any relevant editing information. If editing is disabled, this parameter is nil. The keys for this dictionary are listed in “Editing information keys.”
Uncomment line appDelegate.pickerController.allowsEditing = NO;
and editInfo will not be nil.
精彩评论