How to generate video thumbnail iOS without network call to server? - ios

I want is that when user pick the video from gallery i want to get the thumbnail image of the video.So is it possible to get the thumbnail image of the video without uploading it to server?

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSURL *urlvideo = [info objectForKey:UIImagePickerControllerMediaURL];
__block NSData *movieData = [[NSData alloc]initWithContentsOfURL:urlvideo];AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:urlvideo options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform=TRUE;
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);
}
UIImage *img = [UIImage imageWithCGImage:im];
};
}

Related

iOS: Does writeImageToSavedPhotosAlbum compress the origin image?

I'm using UImagePickerController to take a photo from the camera on iOS9. Then I found that using writeImageToSavedPhotosAlbum to save the image is different from saving the image using UIImageJPEGRepresentation at the file size. Code as follow:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)info {
if( [picker sourceType] == UIImagePickerControllerSourceTypeCamera ) {
[picker dismissViewControllerAnimated:YES completion:nil];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageToSavedPhotosAlbum:image.CGImage orientation:(ALAssetOrientation)image.imageOrientation completionBlock:^(NSURL *assetURL, NSError *error ) {
// NSLog(#"url: %#", assetURL.absoluteString);
NSData *data1 = UIImageJPEGRepresentation(image, 1.0);
NSString *path = [NSString stringWithFormat:#"%#%#", NSTemporaryDirectory(), #"/tmp.jpg"];
[data1 writeToFile:path atomically:YES];
[library assetForURL:assetURL resultBlock:^(ALAsset *asset) {
ALAssetRepresentation *rep = [asset defaultRepresentation];
// CGImageRef iref = [rep fullResolutionImage];
NSLog(#"size1: %lu, size2: %lld", data1.length, rep.size);
} failureBlock:^(NSError *error) {
}];
}];
}
}
it prints:
2016-09-08 10:10:56.690658 TestPhotoes[6096:2345233] size1: 5549568, size2: 2288036
You see, the image file saved by UIImageJPEGRepresentation is way much bigger than by writeImageToSavedPhotosAlbum. I was wondering what the writeImageToSavedPhotosAlbum did? Does it compress the origin image? How can I save image using UIImageJPEGRepresentation to achieve the same effect, both image file size, and the image quality?

iOS create thumbnail for long video

Hi i want to create a thumbnail from my video, is 2.46 minutes long and 68 Mb.
I used this code to create the thumbnail :
AVAsset *asset = [AVAsset assetWithURL:videoURL];
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
CMTime time = [asset duration];
time.value = 0;
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return thumbnail;
The issue is the thumbnail is always nil, but if i reduce the video length to 15 secs it will create the thumbnail.
Can i create thumbnail for movie that more than 15 secs?
thanks
Mmmm this way to use AVAssetImageGenerator works:
AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:videoURL options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform=TRUE;
CGFloat videoLenght=((float)asset.duration.value)/((float)asset.duration.timescale);
CMTime thumbTime = CMTimeMakeWithSeconds(videoLenght,2.0);
AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
if (result != AVAssetImageGeneratorSucceeded) {
NSLog(#"couldn't generate thumbnail, error:%#", error);
}else{
UIImage *image=[self.class scaledImage:[UIImage imageWithCGImage:im]];
[self performSelectorOnMainThread:#selector(setVideoImage:) withObject:image waitUntilDone:NO];
}
};
[generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];
(videoLenght,2.0) is the time where the snapshot takes place, in this case half of the video, I do not recommend you to use a fixed screenshot time because if the time is out of bounds the image will be nil.

Getting thumbnail from a video url asynchronously - iOS

There are lot of example codes for getting thumbnail from a video url. But all of them I tried are NOT generating the image asynchronously. So, when it comes to multiple videos in a tableview, app freezes. Please help me to make this thumbnail asynchronously.
-(UIImage *)generateThumbnailIconForVideoFileWith:(NSURL *)contentURL WithSize:(CGSize)size
{
UIImage *theImage = nil;
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:contentURL options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.maximumSize=size;
generator.appliesPreferredTrackTransform = YES;
NSError *err = NULL;
CMTime time = CMTimeMake(100,100); //change whatever you want here.
CGImageRef imgRef = [generator copyCGImageAtTime:time actualTime:NULL error:&err];
theImage = [[UIImage alloc] initWithCGImage:imgRef] ;
CGImageRelease(imgRef);
return theImage;
}
You can also use MPMovieplayerfor generating thumbnail
MPMoviePlayerController *player= [[MPMoviePlayerController alloc]initWithContentURL:Videourl];
self.imgshow.image = [player thumbnailImageAtTime:5.0 timeOption:MPMovieTimeOptionNearestKeyFrame];
[player stop];

thumbnail of a video on clicking video file in iOS simulator

I have a video in gallery. on clicking it tries to open,but instead of that i want to show a thumbnail of video in uiimageview.
AVURLAsset *asset;
if ([url isFileURL]) {
asset = [[AVURLAsset alloc] initWithURL:url options:nil];
}
AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
gen.appliesPreferredTrackTransform = YES;
CMTime time = CMTimeMakeWithSeconds(0.0, 600);
NSError *error = nil;
CMTime actualTime;
CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
UIImage *thumb = [[UIImage alloc] initWithCGImage:image];
UIImage *finalThumpnailImage = [HPLUtility createThumpnailImageFromTheImage:thumb]; //Resized image.
CGImageRelease(image);
return finalThumpnailImage;

Get the Image (UIImage) using asset URL in iOS 7

I have some images in my Photo Album. I have already retrieved the asset urls of all of them.. I want to get a particular image using the asset url... below code gave me asset url..
ALAssetRepresentation *defaultRepresentation = [asset defaultRepresentation];
NSString *uti = [defaultRepresentation UTI];
NSURL *URL = [[asset valueForProperty:ALAssetPropertyURLs] valueForKey:uti];
Now using this url, how can I get the image(UIImage)?
Get Url by this,
NSURL *url= (NSURL*) [[asset valueForProperty:ALAssetPropertyURLs] valueForKey:[[[asset valueForProperty:ALAssetPropertyURLs] allKeys] objectAtIndex:0]];
And get image from url like below.
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
ALAssetRepresentation *rep = [myasset defaultRepresentation];
#autoreleasepool {
CGImageRef iref = [rep fullScreenImage];
if (iref) {
UIImage *image = [UIImage imageWithCGImage:iref];
self.loadedImage = image;
dispatch_async(dispatch_get_main_queue(), ^{
//UIMethod trigger...
});
iref = nil;
}
}
};
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror)
{
NSLog(#"Can't get image - %#",[myerror localizedDescription]);
};
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:yourUrl
resultBlock:resultblock
failureBlock:failureblock];
UIImage *img = [UIImage imageWithCGImage:[[myAsset defaultRepresentation] fullResolutionImage]
Hope this helps

Resources