2011-09-17 17 views
1

我試圖從使用下面的代碼從相冊中挑選後獲取圖像路徑。但它總是將圖像路徑設置爲NULL /崩潰。是什麼原因?有人能幫助我嗎?iPhone:在didFinishPickingImage中沒有獲取圖像路徑URL

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

回答

0

從UIImagePickerController類參考:
editingInfo
含有任何有關編輯信息的字典。如果禁用編輯,則此參數爲零。該字典的鍵在「編輯信息鍵」中列出。

取消註釋行appDelegate.pickerController.allowsEditing = NO;
和editInfo不會爲零。

+0

您好,我註釋掉appDelegate.pickerController.allowsEditing = NO;不過靜止圖像路徑爲空。但我確信我通過在imageview中添加圖像來獲得正確的圖像。圖像路徑URL仍爲NULL。 – Getsy

+0

@Getsy,調試你的應用程序!檢查editInfo是否爲零。然後NSLog整個editInfo看看它有什麼。 –

+0

它不是NULL。它是這樣給出的 - > {UIImagePickerControllerCropRect =「NSRect:{{0,0},{147,140}}」; UIImagePickerControllerEditedImage =「」; UIImagePickerControllerMediaType =「public.image」; UIImagePickerControllerOriginalImage =「」; } – Getsy

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
+0

嗨,此代碼獲取圖像路徑爲NULL。 – Getsy

+0

誰能幫我解決這個問題 ? – Getsy

+0

UIImagePickerControllerMediaURL用於查找視頻文件路徑,而不是映像文件路徑。 – Harish