[英]getting a poster frame from a video with PhotoKit (iOS8)
I want to extract a UIimage from a video asset, to use as a poster. 我想从视频资产中提取UIimage,以用作海报。 According to the doc for the
PHImageManager
, I should be able to use requestImageForAsset:targetSize:contentMode:options:resultHandler:
. 根据
PHImageManager
的文档,我应该能够使用requestImageForAsset:targetSize:contentMode:options:resultHandler:
Quoting from the doc: 从文档中引用:
You can use this method for both photo and video assets—for a video asset, an image request provides a thumbnail image or poster frame.
您可以对照片和视频资产使用此方法-对于视频资产,图像请求提供缩略图或海报框。
That hasn't been my experience though. 那不是我的经验。 Using
requestImageForAsset:targetSize:contentMode:options:resultHandler:
with a video asset, the callback block always returns nil for the image and a nil error. 使用带有视频资产的
requestImageForAsset:targetSize:contentMode:options:resultHandler:
回调块始终为图像返回nil和nil错误。 The info dictionary returned looks is as follow (nothing I could make sense of) 返回的信息字典外观如下(我无法理解)
{ PHImageFileSandboxExtensionTokenKey = "31c0997752ae82ee32953503bd6d9a2436c50fac;00000000;00000000;000000000000001a;com.apple.app-sandbox.read;00000001;01000003;00000000000756cf;/private/var/mobile/Media/DCIM/100APPLE/IMG_0008.MOV"; PHImageFileURLKey = "file:///var/mobile/Media/DCIM/100APPLE/IMG_0008.MOV"; PHImageFileUTIKey = "dyn.ah62d4uv4ge804550"; PHImageResultDeliveredImageFormatKey = 9999; PHImageResultIsDegradedKey = 0; PHImageResultIsInCloudKey = 0; PHImageResultIsPlaceholderKey = 0; PHImageResultRequestIDKey = 26; PHImageResultWantedImageFormatKey = 9999; }
Here is the method I wrote in a PHAsset
category to extract an image from a video PHAsset below. 这是我在
PHAsset
类别中编写的从下面的视频PHAsset中提取图像的方法。 Has anyone been able to make this work? 有人能做这项工作吗?
@implementation PHAsset (util)
-(PHImageRequestID)fullSizeImage: (void(^)(UIImage *image, NSError *error)) resultHandler {
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
PHImageContentMode contentMode = PHImageContentModeAspectFill ;
PHImageManager *imageManager = [PHImageManager defaultManager] ;
CGSize targetSize = PHImageManagerMaximumSize ;
return [imageManager requestImageForAsset:self targetSize:targetSize contentMode:contentMode options:nil resultHandler:^(UIImage *result, NSDictionary *info) {
NSError *error = (NSError*)[info objectForKey:PHImageErrorKey];
if (result == nil) {
NSLog(@"ERROR while fetching fullSizeImage %@, info:\n%@", error, info);
}
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
resultHandler(result, error);
}];
}];
}
@end
I got it to work after substituting 替换后我就可以工作了
CGSize targetSize = PHImageManagerMaximumSize ;
with... 与...
CGSize targetSize = CGSizeMake(self.pixelWidth*ratio, self.pixelHeight*ratio) ;
I haven't seen any relevant documentation, so probably a bug in iOS8.0.x (at the time of this writing, iOS8.1 beta is available but I haven't tested on it) 我还没有看到任何相关文档,所以可能是iOS8.0.x中的一个错误(在撰写本文时,iOS8.1 beta已可用,但我尚未对其进行测试)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.