iPhone:没有在didFinishPickingImage中获取图像路径URL

时间:2011-09-17 17:24:16

标签: iphone

我正在尝试使用以下代码从相册中挑选后获取图像路径。但它始终将图像路径视为NULL / crash。是什么原因?有人能帮助我吗?

    -(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 = [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);
}

2 个答案:

答案 0 :(得分:2)

你想:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
   NSURL* imageUrl = [info valueForKey:UIImagePickerControllerMediaURL];
   selectedImage = [imageUrl path];
   NSLog(@"selectedImage: %@", selectedImage);
}

您尝试的内容存在一些问题:

  1. imagePickerController:didFinishPickingImage:editingInfo:已弃用
  2. UIImagePickerControllerMediaURL这样的键是常量,应该按原样使用,而不是作为字符串引用。
  3. 您可能需要UIImagePickerControllerMediaURL而不是UIImagePickerControllerReferenceURL

答案 1 :(得分:0)

从UIImagePickerController类引用:
editingInfo
包含任何相关编辑信息的字典。如果禁用编辑,则此参数为nil。此词典的键列在“编辑信息键”中
取消注释行appDelegate.pickerController.allowsEditing = NO;
和editInfo不会是零。