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
Hugo
  • 974
  • 9
  • 21

1 Answers1

2

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)

Hugo
  • 974
  • 9
  • 21