1

我想从 Live Photo 中获取NSArray所有UIImage内容来创建一个 GIF。我尝试在为实时照片制作动画时制作屏幕截图,但它不起作用。谁能帮我?谢谢!

4

2 回答 2

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];
                                   }];
    }
于 2017-08-09T13:44:00.850 回答
1

第一步,您需要使用以下方法将实时照片转换为视频:

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

希望这会帮助你。

于 2016-06-13T15:46:39.410 回答