簡體   English   中英

從實時照片獲取所有圖像

[英]Get all Images from Live Photo

我想從Live Photo獲取帶有所有UIImageNSArray來創建它的GIF。 我在制作實時照片的動畫時嘗試制作屏幕截圖,但它不起作用。 誰能幫我? 謝謝!

第一步,您需要使用以下方法將實時照片轉換為視頻:

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

希望這會幫助你。

這就是我為實現您要求的相同目的所做的。

    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];
                                   }];
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM