I am a newbie in iOS.I want to show a video thumbnail in image view such that by clicking a Button it should open a gallery and by selecting a video it should display the video thumbnail...... please help me...
There are videos and images that stored on photo library, firstly you want to get all videos from the library, prior to iOS 8, this post shows you how to fetch pictures using AssetsLibrary library. You can filter videos out when doing it, then you get an ALAsset, which can represent a image or video, suppose when you select a video, you get a ALAsset named asset, using the below function will get you a thumbnail image for that video.
If you use ALAsset.
UIImage *thumbnail = [UIImage imageWithCGImage:[asset thumbnail]];
For iOS 8, apple introduces a new Photos library,you can fetch all videos from the library as above, a video or images is represented by PHAsset, suppose you have a PHAsset ivar named asset, you can use the following method to get a UIImage from this asset. Take a look at requestImageForAsset:targetSize:contentMode:options:resultHandler:, you can set the target size to the size of the thumb image you want,in the resultHandler,you get the UIImage.
I am using below code to get thumbnail of video which is downloaded in document directory,
- (UIImage *)thumbnailFromVideoAtURL:(NSURL *)url
{
AVURLAsset *aImgAsset = [[AVURLAsset alloc] initWithURL:url options:nil];
AVAssetImageGenerator *aImgGenerator = [[AVAssetImageGenerator alloc] aImgAsset];
aImgGenerator.appliesPreferredTrackTransform = YES;
NSError *err = NULL;
CMTime time = CMTimeMake(1, 2);
CGImageRef aImgRef = [aImgGenerator copyCGImageAtTime:time actualTime:NULL error:&err];
UIImage *aImgThumb = [[UIImage alloc] initWithCGImage:aImgRef];
return aImgThumb;
}
Related
How to get one photo snap before recording video by using UIImagePickerController with media type kUTTypeMovie.
Here i need a photo from my camera before start recording video.
Please suggest me to achieve.
hello you just record the video and extract the first frame or any time interval frame
AVURLAsset* asset = [AVURLAsset URLAssetWithURL:fileURL options:nil];
AVAssetImageGenerator* imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
[imageGenerator setAppliesPreferredTrackTransform:TRUE];
UIImage* image = [UIImage imageWithCGImage:[imageGenerator copyCGImageAtTime:CMTimeMake(0, 1) actualTime:nil error:nil]];
[self.imageView setImage:image];
Am using JSQMessageViewController for chatting,while capturing the video it is saved in the photo album,i can't add it in to the chatview,if am trying to add the video means that video displayed in the png format.So could you please any one help me to solve this issue,How to add the capture the video from the chatview using JSQMessageViewController.After that i would like to upload the video in the chatview is using the API.Every captured video will be saved and added to the API and display the ChatView.
This code is only for create a thumbnail that you want to display in chat
YOURIMAGEVIEW.image = [SELF imageFromVideoUrl:#"GIVE HERE URL OF VIDEO"];
+(UIImage *)imageFromVideoUrl : (NSURL *)videoUrl
{
AVAsset *asset = [AVAsset assetWithURL:videoUrl];
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
imageGenerator.appliesPreferredTrackTransform = YES;
CMTime time = [asset duration];
time.value = 0;
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef); // CGImageRef won't be released by ARC
return thumbnail;
}
When you display this thumbnail in chat and in background call your api that upload a video into your server database . hope that you are clear with my this answer and got a idea what task actually you do.
Happy Coding.
I have several videos uploaded on Amazon S3 bucket. I want to display the videos in a list format in an iOS application along with the thumbnail.
Suppose the url is https://testurlatamzons3/mybucket/somefile.mp4 and I want to get the thumbnail from the URL without actually downloading or streaming the file.
I have seen certain examples and I am able to download the UIImage and display it on the UIImageView. The example I found was here
But for a 350MB file on amazon s3, it takes around 1.9mb of data transfer just to get the thumbnail. Is there a more optimized or a different approach to getting the thumbnails for the mp4/mov files hosted on Amazon S3
Regards,
Nirav
you could get thumbnails using AVFoundation with any time, any size you want:
AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:yourVideoUrl options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform = YES;
CMTime thumbTime = CMTimeMakeWithSeconds(5,30);
AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime,
CGImageRef im,
CMTime actualTime,
AVAssetImageGeneratorResult result,
NSError *error){
NSLog(#"make sure generator is used in this block and it'll work %#", generator);
};
generator.maximumSize = CGSizeMake(320, 180);
[generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];
I guess you have two options:
To have some API which does what you want and returns the URL of the thumbnail image to your application
To download just a small part of the video and grab a frame from it but this depends on the format of your video, maybe this could help for MP4 videos
https://github.com/ohminteractive/OHMTagLib
I'm developing and iOS app for iPad and I'm using a Repository called Grabkit in order to get images from different services like Instagram and Flicker in addition to images from the Camera Roll. The problem is that when the user selects a picture from the roll I get and URL such this: assets-library://asset/asset.JPG?id=DCFB9E49-93AA-49E3-89C8-2EE64AE2C4C6&ext=JPG
I've tried some codes to get the image from this kind of paths but no one has worked, such as the following:
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
// Ask for the "Asset" for the URL. An asset is a representation of an image in the Photo application.
[library assetForURL:originalImage.URL
resultBlock:^(ALAsset *asset) {
// Here, we have the asset, let's retrieve the image from it
CGImageRef imgRef = [[asset defaultRepresentation] fullResolutionImage];
/* Instead of the full res image, you can ask for an image that fits the screen
CGImageRef imgRef = [[asset defaultRepresentation] fullScreenImage];
*/
// From the CGImage, let's build an UIImage
imatgetemporal = [UIImage imageWithCGImage:imgRef];
} failureBlock:^(NSError *error) {
// Something wrong happened.
}];
Is something in my code wrong? Must I try another code?
I am recording a video from the iPhone camera by using the AVCam code provided from apple.
After the video is recorded it is saved to the photos library.
A new view is then loaded, here I need to have an image thumbnail of the video.
I have a path to the video:
file://localhost/private/var/mobile/Applications/ED45DEFC-ABF9-4A5E-9102-21680CC1448E/tmp/output.mov
I can't seem to figure how to get the first frame of the video to use as a thumbnail.
Any help would be very appreciated and thank you for your time.
EDIT
I ended up using this, I'm not sure why it returns the image sideways?
- (UIImage*)loadImage {
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:vidURL options:nil];
AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:asset];
NSError *err = NULL;
CMTime time = CMTimeMake(1, 60);
CGImageRef imgRef = [generate copyCGImageAtTime:time actualTime:NULL error:&err];
NSLog(#"err==%#, imageRef==%#", err, imgRef);
return [[UIImage alloc] initWithCGImage:imgRef];
}
To fix the thumbnail orientation set appliesPreferredTrackTransform to YES in the AVAssetImageGenerator instance. If you add your own video composition, you'll need to include the right transform to rotate the video as wanted.
generate.appliesPreferredTrackTransform = YES;
Remember to release the obtained image reference with CGImageRelease.
To request multiple thumbnails it's better to do asynchronously with generateCGImagesAsynchronouslyForTimes:completionHandler:.