Getting a thumbnail from a video url or data in iOS
The answer to this question is that one can now with 4.0 iOS get thumbnails using AVFoundation, the following code where the class property url is the movie url, will do the trick (you can get the thumbnail at any time, in the example its at time 0)
-(void)generateImage
{
AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:self.url options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform=TRUE;
[asset release];
CMTime thumbTime = CMTimeMakeWithSeconds(0,30);
AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
if (result != AVAssetImageGeneratorSucceeded) {
NSLog(@"couldn't generate thumbnail, error:%@", error);
}
[button setImage:[UIImage imageWithCGImage:im] forState:UIControlStateNormal];
thumbImg=[[UIImage imageWithCGImage:im] retain];
[generator release];
};
CGSize maxSize = CGSizeMake(320, 180);
generator.maximumSize = maxSize;
[generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];
}
Creating thumbnail from local video in swift
Translated with some edits from:
First frame of a video using AVFoundation
var err: NSError? = nil
let asset = AVURLAsset(URL: NSURL(fileURLWithPath: "/that/long/path"), options: nil)
let imgGenerator = AVAssetImageGenerator(asset: asset)
let cgImage = imgGenerator.copyCGImageAtTime(CMTimeMake(0, 1), actualTime: nil, error: &err)
// !! check the error before proceeding
let uiImage = UIImage(CGImage: cgImage)
let imageView = UIImageView(image: uiImage)
// lay out this image view, or if it already exists, set its image property to uiImage
Related Topics
How to Debug Memory Leaks When Leaks Instrument Does Not Show Them
How to Create Dispatch Queue in Swift 3
Generate Json String from Nsdictionary in Ios
Detect Backspace in Empty Uitextfield
How to Force a Uiviewcontroller to Portrait Orientation in iOS 6
Ios 9 Not Opening Instagram App With Url Scheme
How to Present Uialertcontroller When Not in a View Controller
How to Find Topmost View Controller on Ios
How to Adjust the Anchor Point of a Calayer, When Auto Layout Is Being Used
How to Take a Screen Shot of a Uiview
Ios App With Framework Crashed on Device, Dyld: Library Not Loaded, Xcode 6 Beta
How to Use Scnetworkreachability in Swift
Change User Agent in Uiwebview
Uicolor Not Working With Rgba Values