I am using a custom image picker to pick a time lapse and I want to get the url of the picked time lapse so I can then play the time lapse.
- (void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didFinishPickingAssets:(NSArray *)assets {
for (PHAsset *asset in assets) {
NSURL *url = [NSURL URLWithString: assetURLgoesHere];
AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:avAsset];
AVPlayer *videoPlayer = [AVPlayer playerWithPlayerItem:playerItem];
[videoPlayer play];
}
[self dismissViewControllerAnimated:YES completion:NULL];
}
This is what I have at the moment but I need to get the url of the asset.
I usually use swift but this custom image picker is written in objective-c. so I am a bit of a objective-c noob
Updated code
- (void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didFinishPickingAssets:(NSArray *)assets {
for (PHAsset *asset in assets) {
- (PHImageRequestID)requestAVAssetForVideo:(PHAsset *)asset options:(PHVideoRequestOptions *)options resultHandler:(void (^)(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info))resultHandler
NSURL *url = [NSURL URLWithString: asset];
AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:avAsset];
AVPlayer *videoPlayer = [AVPlayer playerWithPlayerItem:playerItem];
[videoPlayer play];
}
[self dismissViewControllerAnimated:YES completion:NULL];
}
By the way, if you want the actual URL--that is to say, the same one that UIImagePickerController passes to an AVPlayerItem when it is created, and the same one that looks like the URL one would use with AVAssetReader/Writer--then do this:
[[PHImageManager defaultManager] requestAVAssetForVideo:phAsset options:nil resultHandler:^(AVAsset *avAsset, AVAudioMix *audioMix, NSDictionary *info) {
NSURL *url = (NSURL *)[[(AVURLAsset *)avAsset URL] fileReferenceURL];
NSLog(@"url = %@", [url absoluteString]);
NSLog(@"url = %@", [url relativePath]);
}];
Whereas phAsset is the PHAsset object, and avAsset is the resulting AVAsset object generated by PHImageManager, the output to the console from the above code will produce, for example:
2016-04-16 01:15:40.155 ChromaEpsilon[3423:933358] url = file:///.file/id=16777218.8262005
2016-04-16 01:15:40.155 ChromaEpsilon[3423:933358] url = /private/var/mobile/Media/DCIM/108APPLE/IMG_8421.MOV
You should use PHImageManager
.
Use the class's sharedInstance
and call this method with your PHAsset
, options, and a completion block handler:
- (PHImageRequestID)requestAVAssetForVideo:(PHAsset *)asset options:(PHVideoRequestOptions *)options resultHandler:(void (^)(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info))resultHandler
The handler will give you an AVAsset
you should use for your AVPlayerItem
, as opposed to a URL.
Example:
[[PHImageManager defaultManager] requestAVAssetForVideo:asset options:nil resultHandler:^(AVAsset *avAsset, AVAudioMix *audioMix, NSDictionary *info) {
// Use the AVAsset avAsset
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:avAsset];
AVPlayer *videoPlayer = [AVPlayer playerWithPlayerItem:playerItem];
}];
Beware its likely asynchronous which will interfere with your application flow further.
For Swift 3.0 You can get string url for image and video from PHAsset object
asset.requestContentEditingInput(with: PHContentEditingInputRequestOptions(), completionHandler: { (contentEditingInput, dictInfo) in
if asset.mediaType == .image
{
if let strURL = contentEditingInput?.fullSizeImageURL?.absoluteString
{
print("IMAGE URL: ", strURL)
}
}
else
{
if let strURL = (contentEditingInput!.avAsset as? AVURLAsset)?.url.absoluteString
{
print("VIDEO URL: ", strURL)
}
}
})
Proper answer of @HirenPanchal in Swift 4.0(also working in Swift 5.0+) and iOS 9.0+
func getUrlFromPHAsset(asset: PHAsset, callBack: @escaping (_ url: URL?) -> Void)
{
asset.requestContentEditingInput(with: PHContentEditingInputRequestOptions(), completionHandler: { (contentEditingInput, dictInfo) in
if let strURL = (contentEditingInput!.audiovisualAsset as? AVURLAsset)?.url.absoluteString
{
PRINT("VIDEO URL: \(strURL)")
callBack(URL.init(string: strURL))
}
})
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With