2014-05-22 2 views
2

AVAssetImageGenerator 클래스를 사용하여 썸네일 이미지를 생성했지만이 썸네일 이미지를 UITableViewCell 크기 (320,239)의 UIImageView에 맞출 필요가 있습니다. AVAssetImageGenerator 최대 크기 속성을 (320,239)로 지정했는데 원하는 크기를 얻지 못했습니다. 사전에 감사드립니다. 빠른 참조를 위해 다음 코드를 살펴보십시오.AVAssetImageGenerator를 사용하여 생성 된 썸네일 이미지의 이미지 크기를 설정했습니다.

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); 

내가

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

답변

2

아무것도 코드에서 잘못은 없지만, 같이이 문서에 언급 된 내 scenario.`에하지만 훨씬 도움이되지 "유래"에서 가져온 또 하나 개의 솔루션

AVAssetImageGenerator는 정의 된 경계 상자 내에 들어갈 수 있도록 이미지의 크기를 조절합니다. 이미지는 절대로 확대되지 않습니다. 크기가 조절 된 이미지 의 종횡비는 apertureMode 속성에 의해 정의됩니다. apertureMode 이후

이 비율을 유지 생성 AVAssetImageGeneratorApertureModeProductionAperture 화상으로 설정된다. 동영상과 크기 세트의 가로 세로 비율이 같을 경우에만 최대 크기에 맞춰지며 지정된 크기로는 발생하지 않습니다.
가 잘립니다 수 UIImageViewcontentModeUIViewContentModeScaleAspectFill 일부 내용을 변경하려고하지만 전체 이미지 뷰

+0

을 채울 것입니다, 내가 이것에 대해 알고 빠른 재생을위한 감사, 최대 크기는 가로 세로 비율에 따라 달라집니다, 어떻게 설정하는 또 하나의 의심 프레임 시간을 사용하여 CMTimeMakeWithSeconds. 제발 내가 이것에 대해 설명하지만 여전히 명확하지 않습니다. 3 초 후에 프레임을 가져 가야할까요? 미리 설정해 주셔서 감사합니다. – sunny

+0

CMTime의 의미는 매우 깊습니다. 그것에 대한 더 많은 정보를 원한다. (그리고해야한다) 여기 튜토리얼 http://warrenmoore.net/understanding-cmtime이있다. 일단 당신이 그것을 읽으면 3 초 후에 프레임을 잡으면 쉽게 CMTime time = CMTimeMakeWithSeconds (3.0, 1); – Andrea

+0

대단히 감사합니다. 링크를 통해갑니다. – sunny