0

I want to extract a UIimage from a video asset, to use as a poster. According to the doc for the PHImageManager, I should be able to use 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. 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. 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
4

1 回答 1

2

替换后我让它工作了

CGSize targetSize = PHImageManagerMaximumSize ;

和...

CGSize targetSize = CGSizeMake(self.pixelWidth*ratio, self.pixelHeight*ratio) ;

我没有看到任何相关文档,所以可能是 iOS8.0.x 中的一个错误(在撰写本文时,iOS8.1 beta 可用但我尚未对其进行测试)

于 2014-10-15T17:44:16.120 回答