从Live Photo获取所有图像

时间:2016-06-13 12:16:13

标签: objective-c xcode phlivephoto apple-live-photos

我希望获得一张NSArray来自动态照片中的所有UIImage来创建一个GIF。我试图在制作实时照片动画时制作屏幕截图,但它不起作用。 谁能帮我? 谢谢!

2 个答案:

答案 0 :(得分:1)

首先,您需要将Live Photo转换为视频,使用:

PHAssetResourceManager.defaultManager().writeDataForAssetResource(assetRes, 
    toFile: fileURL, options: nil, completionHandler: 
  {
     // Video file has been written to path specified via fileURL
  }

最后,使用此库将其转换为GIF,或者您可以在google中搜索其他方式:https://github.com/NSRare/NSGIF

希望这会对你有所帮助。

答案 1 :(得分:1)

这就是我为实现您所要求的目标所做的一切。

    PHFetchOptions *options = [[PHFetchOptions alloc] init];
    options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
    options.predicate = [NSPredicate predicateWithFormat:@"mediaType == %d", PHAssetMediaTypeImage];
    options.predicate = [NSPredicate predicateWithFormat:@"mediaSubtype == %d", PHAssetMediaSubtypePhotoLive];
    options.includeAllBurstAssets = NO;
    PHFetchResult *allLivePhotos = [PHAsset fetchAssetsWithOptions:options];
    NSLog(@"Get total live count : %ld",(unsigned long)allLivePhotos.count);
    NSMutableArray *arrAllLiveImagesGroups = [NSMutableArray array];

    for (PHAsset *asset in allLivePhotos) {
        [asset requestContentEditingInputWithOptions:nil
                                   completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
                                       NSURL *urlMov = [contentEditingInput.livePhoto valueForKey:@"videoURL"];

                                       NSMutableArray *arrLive = [NSMutableArray array];
                                       NSMutableArray *arrSingleLiveImagesGroup = [NSMutableArray array];
                                       AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:urlMov options:nil];
                                       AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
                                       generator.requestedTimeToleranceAfter =  kCMTimeZero;
                                       generator.requestedTimeToleranceBefore =  kCMTimeZero;

                                       for (Float64 i = 0; i < CMTimeGetSeconds(asset.duration) *  5 ; i++){
                                           @autoreleasepool {
                                               CMTime time = CMTimeMake(i, 5);
                                               NSError *err;
                                               CMTime actualTime;
                                               CGImageRef image = [generator copyCGImageAtTime:time actualTime:&actualTime error:&err];
                                               UIImage *generatedImage = [[UIImage alloc] initWithCGImage:image scale:1.0 orientation:UIImageOrientationDown];
                                               [arrLive addObject:generatedImage];
                                               CGImageRelease(image);
                                           }
                                       }
                                       [arrSingleLiveImagesGroup addObject:arrLive];
                                       [arrAllLiveImagesGroups addObject:arrSingleLiveImagesGroup];
                                   }];
    }