2014-05-22 46 views
2

我正在使用AVAssetImageGenerator類來生成縮略圖圖像,但問題是我需要將該縮略圖圖像放入具有尺寸爲(320,239)的UIImageViewUITableViewCell。 我指定AVAssetImageGenerator最大尺寸的財產爲(320,239),但我沒有得到所需的大小我指定的任何幫助,請提前感謝。請看下面的代碼以便快速參考。使用AVAssetImageGenerator生成的縮略圖圖像的設置圖像大小ios

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

還有一個解決方案,我從「計算器」,但沒有多少幫助的得到了我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]; 

回答

2

沒有什麼是錯在你的代碼,但如文檔中表示

AVAssetImageGenerator縮放圖像,使其適合 定義的邊界框。圖像永遠不會放大。縮放圖像的長寬比由apertureMode屬性定義。

由於apertureMode被設置爲產生保持縱橫比AVAssetImageGeneratorApertureModeProductionAperture圖像。只有當視頻和尺寸設置具有相同的縱橫比時,它纔會適合最大尺寸,並且在指定尺寸時不會發生這種情況。
嘗試改變UIImageViewcontentModeUIViewContentModeScaleAspectFill一些內容可以裁剪,但你會充滿整個ImageView的

+0

,得益於快速重播我知道這件事,最大大小取決於縱橫比,我還有一個疑問,如何設置框架時間使用CMTimeMakeWithSeconds.Please解釋關於這個我讀的文檔,但仍然不清楚。假設我需要在3秒後採取框架如何設置它你能解釋提前感謝 – sunny

+0

CMTime的意義是非常深刻的,如果你想要更多的信息(你應該)在這裏是一個教程http://warrenmoore.net/understanding-cmtime。一旦你讀了它,你可以明白,在3秒後抓住一個框架,你可以很容易地使用CMTime time = CMTimeMakeWithSeconds(3.0,1); – Andrea

+0

非常感謝你,我會通過鏈接 – sunny