iOS7編程Cookbook中例15.8中一個小問題
該書的15.8例子標題為Editing Videos on an iOS Device,代碼的功能為建立一個UIImagePickerController視圖讓使用者從照片庫選擇一個視頻檔案,然後在UIVideoEditorController視圖中編輯該視頻,最後得到編輯後視頻檔案的路徑.
很好很簡單,但是在實際運行代碼中發現當UIImagePickerController返回後,在imagePickerController:(UIImagePickerController )picker didFinishPickingMediaWithInfo:(NSDictionary
NSString *mediaType = info[UIImagePickerControllerMediaType]; if ([mediaType isEqualToString:(__bridge NSString*)kUTTypeMovie]) { _videoURLToEdit = info[UIImagePickerControllerMediaURL]; }
即執行後的_videoURLToEdit總為nil,即使前面的資源類型是正確的:為kUTTypeMovie類型.
網上搜了一下,發現很多人說在iOS9裡面都是這樣,包括Stack OF裡面也是如此.但不怎麼相信此說法…
後來到Apple SDK中尋找info字典的解釋:
info
A dictionary containing the original image and the edited image, if an image was picked; or a filesystem URL for the movie, if a movie was picked. The dictionary also contains any relevant editing information. The keys for this dictionary are listed in Editing Information Keys.
可以看到其所有key的解釋:
NSString *const UIImagePickerControllerMediaType;NSString *const UIImagePickerControllerOriginalImage;NSString *const UIImagePickerControllerEditedImage;NSString *const UIImagePickerControllerCropRect;NSString *const UIImagePickerControllerMediaURL;NSString *const UIImagePickerControllerReferenceURL;NSString *const UIImagePickerControllerMediaMetadata;NSString *const UIImagePickerControllerLivePhoto;
注意看其中包含一個UIImagePickerControllerReferenceURL枚舉,其說明稱該key的值為原始資源的URL.而UIImagePickerControllerMediaURL裡的值是當原始資源被修改後的URL.
我們前面總是返回nil,是因為我們沒有修改原始資源,所以總為空白值.
我們簡單的將代碼修改如下即可:
if ([mediaType isEqualToString:(__bridge NSString*)kUTTypeMovie]) { _videoURLToEdit = info[UIImagePickerControllerMediaURL]; if (!_videoURLToEdit) { _videoURLToEdit = info[UIImagePickerControllerReferenceURL]; } }