Tôi đang sử dụng mã dựa trên mã trong các chủ đề sau đây để tạo ra một hình thu nhỏ video:Tạo Thumbnail từ Video - Cải thiện Hiệu suất Tốc độ - AVAsset - iPhone
Getting a thumbnail from a video url or data in iPhone SDK
Mã của tôi là như sau:
if (selectionType == kUserSelectedMedia) {
NSURL * assetURL = [info objectForKey:UIImagePickerControllerReferenceURL];
AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:assetURL options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform=TRUE;
[asset release];
CMTime thumbTime = CMTimeMakeWithSeconds(0,30);
//NSLog(@"Starting Async Queue");
AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
if (result != AVAssetImageGeneratorSucceeded) {
NSLog(@"couldn't generate thumbnail, error:%@", error);
}
//NSLog(@"Updating UI");
selectMediaButton.hidden = YES;
selectedMedia.hidden = NO;
cancelMediaChoiceButton.hidden = NO;
whiteBackgroundMedia.hidden = NO;
//Convert CGImage thumbnail to UIImage.
UIImage * thumbnail = [UIImage imageWithCGImage:im];
int checkSizeW = thumbnail.size.width;
int checkSizeH = thumbnail.size.height;
NSLog(@"Image width is %d", checkSizeW);
NSLog(@"Image height is %d", checkSizeH);
if (checkSizeW >=checkSizeH) {
//This is a landscape image or video.
NSLog(@"This is a landscape image - will resize automatically");
}
if (checkSizeH >=checkSizeW) {
//This is a portrait image or video.
selectedIntroHeight = thumbnail.size.height;
}
//Set the image once resized.
selectedMedia.image = thumbnail;
//Set out confirm button BOOL to YES and check if we need to display confirm button.
mediaReady = YES;
[self checkIfConfirmButtonShouldBeDisplayed];
//[button setImage:[UIImage imageWithCGImage:im] forState:UIControlStateNormal];
//thumbImg=[[UIImage imageWithCGImage:im] retain];
[generator release];
};
CGSize maxSize = CGSizeMake(320, 180);
generator.maximumSize = maxSize;
[generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];
}
}
Vấn đề là có sự chậm trễ khoảng 5-10 giây khi tạo hình thu nhỏ. Có anyway mà tôi có thể cải thiện tốc độ của mã này và tạo ra các hình thu nhỏ nhanh hơn?
Cảm ơn bạn.
Phần lớn nhanh hơn - cảm ơn bạn. – GuybrushThreepwood
+1 nó đã giúp tôi cũng – Charan
Tôi không nhận được hình thu nhỏ cho url này: http://www.sciencentral.com/news/image_db/2024515/NNM2212_AntPower_MSTR.mov ..Bạn có thể kiểm tra không? – parilogic