set image size for thumbnail image generated using AVAssetImageGenerator ios

4k views Asked by At

I was using AVAssetImageGenerator class for generating thumbnail image but problem is i need to fit that thumbnail image into UITableViewCell having UIImageView of size (320,239). I specified AVAssetImageGenerator maximum size property as (320,239) but i was not getting desired size what i specified please any help thanks in advance. Please look at the following code for quick reference.

    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:self.consenteeVideoUrl options:nil];

AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];

gen.maximumSize = CGSizeMake(320, 239);

gen.apertureMode = AVAssetImageGeneratorApertureModeProductionAperture;

gen.appliesPreferredTrackTransform = YES;

CMTime time = CMTimeMakeWithSeconds(0.0, 600);

NSError *error = nil;

CMTime actualTime;

CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];

UIImage *thumbnailImage = [[UIImage alloc] initWithCGImage:image];
//thumbnailImage = [CSUtilities imageWithImage:thumbnailImage scaledToSize:CGSizeMake(320, 239)];
CGImageRelease(image);

One more solution i got from "stackoverflow" but not much helpful in my scenario.`

AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:self.consenteeVideoUrl options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform=TRUE;

CMTime thumbTime = CMTimeMakeWithSeconds(30,30);

AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
    if (result != AVAssetImageGeneratorSucceeded) {
        NSLog(@"couldn't generate thumbnail, error:%@", error);
    }

    UIImage *thumbnailImage = [[UIImage alloc] initWithCGImage:im];
    //save image to application directory...
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:self.uniqueCodeString];
    if (![[NSFileManager defaultManager] fileExistsAtPath:folderPath])
        [[NSFileManager defaultManager] createDirectoryAtPath:folderPath  withIntermediateDirectories:NO attributes:nil error:&error];

    NSString *savedImagePath = [folderPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",self.uniqueCodeString]];
    NSData *imageData = UIImagePNGRepresentation(thumbnailImage);
    BOOL success = [imageData writeToFile:savedImagePath atomically:YES];
    if (success) {
        CSLog(@"sucess");
    }
    partnerEvent.thumbnailImageStr = [self getImagePath];
    [self.ibPartnerEventTableView reloadData];
 };

 CGSize maxSize = CGSizeMake(320, 239);
 generator.maximumSize = maxSize;
 [generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];
1

There are 1 answers

3
Andrea On

Nothing is wrong in your code, but as stated in the documentation

AVAssetImageGenerator scales images such that they fit within the defined bounding box. Images are never scaled up. The aspect ratio of the scaled image is defined by the apertureMode property.

Since the apertureMode is set to AVAssetImageGeneratorApertureModeProductionAperture images generated keeps the aspect ratio. it will only fit the maximum size if the the video and the size set have the same aspect ratio, and this will unlikely to happen with the size specified.
Try to change the UIImageView contentMode to UIViewContentModeScaleAspectFill some content could be cropped but you will fill the entire imageView