Thumbnail image of video from ALAssetsLibrary in Swift?

We are fetching all images and Video from PhotoLibrary using ALAssetLibrary. This library includes all Saved photos , directly import from device and also from iTunes. 

Here, we are discussing, only to get the thumbnail image of video which comes from PhotoLibrary.

This tutorial provide code for objective C and Swift.

Swift  : code for Thumbnail of  Video : 
func generateThumbImage(url : NSURL) -> UIImage{
        var asset : AVAsset = AVAsset.assetWithURL(url) as! AVAsset
        var assetImgGenerate : AVAssetImageGenerator = AVAssetImageGenerator(asset: asset)
        assetImgGenerate.appliesPreferredTrackTransform = true
        var error       : NSError? = nil
        var time        : CMTime = CMTimeMake(1, 30)
        var img         : CGImageRef = assetImgGenerate.copyCGImageAtTime(time, actualTime: nil, error: &error)
        var frameImg    : UIImage = UIImage(CGImage: img)!

        return frameImg

Objective C : code for Thumbnail of  Video :
-(UIImage *)generateThumbImage : (NSString *)filepath
    NSURL *url = [NSURL fileURLWithPath:filepath];

    AVAsset *asset = [AVAsset assetWithURL:url];
    AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
self.imageGenerator.appliesPreferredTrackTransform = YES; 
        CMTime time = [asset duration];
        time.value = 0;
        Float duration = CMTimeGetSeconds([myAsset duration]);
   for(Float i = 0.0; i<duration; i=i+0.1)
     CGImageRef imgRef = [self.imageGenerator copyCGImageAtTime:CMTimeMake(i, duration) actualTime:NULL error:nil];
     UIImage* thumbnail = [[UIImage alloc] initWithCGImage:imgRef scale:UIViewContentModeScaleAspectFit orientation:UIImageOrientationUp];
    [thumbnailImages addObject:thumbnail];


Thumbnail image of video from ALAssetsLibrary in Swift? Thumbnail image of video from ALAssetsLibrary in Swift? Reviewed by Kirit Modi on 02:46:00 Rating: 5

No comments:

Powered by Blogger.