How to get thumbnail image of video from ALAsset in iOS?
Asked Answered
B

4

5

I want to get thumbnail images of every frame from video and then save this images in Mutable Array of images.

I want to use this images to play as a animation.

NSURL* assetURL = [self.asset valueForProperty:ALAssetPropertyAssetURL];
NSDictionary* assetOptions = nil;

AVAsset* myAsset = [[AVURLAsset alloc] initWithURL:assetURL options:assetOptions];

self.imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:myAsset];


int duration = CMTimeGetSeconds([myAsset duration]);

for(int i = 0; i<duration; i++)
{
    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];
}

I am using above code to get thumbnail images but the problem is if there is a 2 seconds video i am only getting 2 thumbnails but i want 20 thumbnails (10 thumbnail per second).

So, how to use CMTimeMake to get thumbnails for every .1 second

Bess answered 7/7, 2015 at 7:42 Comment(0)
G
8

Code form reference site : Thumbnail image from Video

Objective - C

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

Swift

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
    }
Gowon answered 7/7, 2015 at 7:53 Comment(6)
Hey, your code returns only one image. I want array of all thumbnail images. ThanksBess
Then its not Called Thumb image.Gowon
like you generated one image at any given time, i want multiple images from diff. time and show as animationBess
hey i updated my question according to your code, but there is a little logical problem. Can you help me?Bess
@lalit Kumar , Change your for loop for(float i = 0.0,i<duration; i=i+0.1) may be get all thumbs.Gowon
Thanks. can you help me on this question https://mcmap.net/q/2031771/-how-to-play-multiple-videos-smoothly-in-ios-in-uicollectionview/3976183Bess
C
1

@Kirit Modi solution in Swift 3 with some small changes:

func generateThumbImage(url : URL) -> UIImage?{

  let asset = AVAsset(url: url)
  let assetImgGenerate : AVAssetImageGenerator = AVAssetImageGenerator(asset: asset)
assetImgGenerate.appliesPreferredTrackTransform = true
  let time = CMTimeMake(1, 30)
  let img = try? assetImgGenerate.copyCGImage(at: time, actualTime: nil)

  guard let cgImage = img else { return nil }

  let frameImg    = UIImage(cgImage: cgImage)
  return frameImg
}
Corpuz answered 24/2, 2017 at 17:4 Comment(0)
C
0

Following code for the getting the thumb image from video at 0.41

NSString *str = [[self.videoArray objectAtIndex:i] valueForKey:@"vName"];
NSURL *videoURL = [NSURL URLWithString:str] ;
MPMoviePlayerController *player = [[[MPMoviePlayerController alloc] initWithContentURL:videoURL]autorelease];
UIImage  *thumbnail = [player thumbnailImageAtTime:0.41 timeOption:MPMovieTimeOptionNearestKeyFrame];
player = nil;
Claudio answered 7/7, 2015 at 7:47 Comment(3)
I want all thumbnail images from asset (which is type of video), not single image.Bess
you need to change the value of the thumbnailImageAtTime:0.41Claudio
I am trying your code also. But i am using ALAsset. So, may you edit your answer in respect to ALAssetBess
L
0
extension UIImageView{
func getImageThumnail(forLink link: String) {
    let asset = AVAsset(url: URL(string: link)!)
    let assetImgGenerate = AVAssetImageGenerator(asset: asset)
    assetImgGenerate.appliesPreferredTrackTransform = true
    let time = CMTimeMakeWithSeconds(Float64(50), 100)
    DispatchQueue.global(qos: .userInteractive).async {
      do {
            let img = try assetImgGenerate.copyCGImage(at: time, actualTime: nil)
            let thumbnail = UIImage(cgImage: img)
            DispatchQueue.main.async {
                self.image = thumbnail
            }
    } catch {
    }
    }
}
}
Luong answered 20/9, 2018 at 11:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.