A small problem in the iOS7 programming Cookbook example 15.8
The title of the 15.8 example in this book is Editing Videos on an iOS Device. The code function is to create a UIImagePickerController view that allows users to select a video file from the photo gallery and then edit the video in the UIVideoEditorController view, finally, get the path of the edited video file.
It is very easy, but in the actual running code, it is found that when UIImagePickerController returns, in the imagePickerController :( UIImagePickerController) Picker didFinishPickingMediaWithInfo :( NSDictionary
NSString *mediaType = info[UIImagePickerControllerMediaType]; if ([mediaType isEqualToString:(__bridge NSString*)kUTTypeMovie]) { _videoURLToEdit = info[UIImagePickerControllerMediaURL]; }
That is, after the execution, the _ videoURLToEdit is always nil, even if the previous resource type is correct: it is of the kUTTypeMovie type.
I searched the internet and found that many people say this is true in iOS9, including the Stack OF. But I don't quite believe this...
Then go to the Apple SDK to find the description of the info dictionary:
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.
We can see the explanation of all its keys:
NSString *const UIImagePickerControllerMediaType;NSString *const UIImagePickerControllerOriginalImage;NSString *const UIImagePickerControllerEditedImage;NSString *const UIImagePickerControllerCropRect;NSString *const UIImagePickerControllerMediaURL;NSString *const UIImagePickerControllerReferenceURL;NSString *const UIImagePickerControllerMediaMetadata;NSString *const UIImagePickerControllerLivePhoto;
Note that there is a UIImagePickerControllerReferenceURL enumeration, which indicates that the key value is the URL of the original resource. The value in UIImagePickerControllerMediaURL is the URL after the original resource is modified.
We always return nil because we have not modified the original resource, so it is always null.
We can simply modify the Code as follows:
if ([mediaType isEqualToString:(__bridge NSString*)kUTTypeMovie]) { _videoURLToEdit = info[UIImagePickerControllerMediaURL]; if (!_videoURLToEdit) { _videoURLToEdit = info[UIImagePickerControllerReferenceURL]; } }