【发布时间】:2014-02-25 02:10:27
【问题描述】:
我正在尝试从视频中获取一组图像。它运行良好,但我怀疑完成处理程序在哪个线程中被调用。
我在新操作中调用了这个方法(
generateCGImagesAsynchronouslyForTimes:),并在完成处理程序中更新了 UI。用户界面得到更新。但 UI 更新通常不会发生在辅助线程中?我怀疑是在当前调用线程还是主线程中调用了完成处理程序?
我的代码是:
__block unsigned int i = 0;
AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
i++;
if(result == AVAssetImageGeneratorSucceeded){
//Create a block to save the image in disk
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *documentsDirectory = [NSHomeDirectory()
stringByAppendingPathComponent:@"Documents"];
NSError *error = nil;
//Create file path for storing the image
NSString *videoOutputPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"VideoFrames%i.png", i]];
//Delete if already any image exist
if ([fileMgr fileExistsAtPath:videoOutputPath]){
if ([fileMgr removeItemAtPath:videoOutputPath error:&error] != YES)
NSLog(@"Unable to delete file: %@", [error localizedDescription]);
}
//Convert the CGImageRef to UIImage
UIImage *image = [UIImage imageWithCGImage:im];//**This line gives error: EXE_BAD_ACCESS**
//Save the image
if(![UIImagePNGRepresentation(image) writeToFile:videoOutputPath options:NSDataWritingFileProtectionNone error:&error])
NSLog(@"Failed to save image at path %@", videoOutputPath);
}];
//Add the operation to the queue
[self.imageWritingQueue addOperation:operation];
}
}
};
【问题讨论】:
标签: ios multithreading nsoperation avassetimagegenerator