gpt4 book ai didi

ios - 从图库中生成所有视频的视频缩略图会导致内存问题

转载 作者:行者123 更新时间:2023-12-01 20:21:50 25 4
gpt4 key购买 nike

我正在开发一个应用程序,我需要在我的画廊中显示所有视频的缩略图(作为收藏 View 查看)。现在我正在使用 AVAssetImageGenerator 从画廊中的视频生成缩略图,但我遇到了内存问题。这是我的我正在使用的代码:

    PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:nil];

PHImageManager *imageManager = [PHImageManager new];

for(NSInteger i=0 ; i < fetchResult.count ; i++){

__weak SAVideosViewController *weakSelf = self;
[imageManager requestAVAssetForVideo:fetchResult[i] options:nil resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {

[collectionViewData addObject:asset];
//my method to generate video thumbnail...
[self generateThumbnailForAsset:asset];

if(i == fetchResult.count-1){

collectionViewDataFilled = YES;

dispatch_async(dispatch_get_main_queue(), ^{

[weakSelf.myCollectionView reloadData];
});

}
}];
}

这是上面调用的方法:
    -(void)generateThumbnailForAsset:(AVAsset*)asset_{

AVAssetImageGenerator *imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset_];
CMTime time = CMTimeMakeWithSeconds(1,1);
CMTimeShow(time);
CGImageRef img = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];

if(img != nil){
NSLog(@"image");
[thumbnails addObject:[UIImage imageWithCGImage:img]];
}

CGImageRelease(img);

}

我想知道为什么我在这里遇到内存问题以及如何解决它。

最佳答案

似乎没有人知道正确的答案。所以在这里我在经过一些研究后回答我自己的问题。

注意:要做这样的事情,您不需要使用 AVAssetImageGenerator。您可以使用以下方法(PHOTOS FRAMEWORK)。

PHCachingImageManager *cachingImageManager;

[cachingImageManager startCachingImagesForAssets:collectionViewData targetSize:cellSize contentMode:PHImageContentModeAspectFill options:nil];

此处使用的类是 PHCachingImageManager。在苹果文档中了解此内容。

然后在此之后,使用第二种方法从缓存中检索数据。
    [cachingImageManager requestImageForAsset:collectionViewData[indexPath.row] targetSize:AssetGridThumbnailSize contentMode:PHImageContentModeAspectFill options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {

cell.myImageView.image = result;
}];

回调处理程序提供了一个图像,您可以在您的集合查看单元格中使用它。在 cellForItemAtIndexPath 方法中使用它。

有关完整的代码和引用,请参阅 APPLE 的此示例。
https://developer.apple.com/library/ios/samplecode/UsingPhotosFramework/Introduction/Intro.html

关于ios - 从图库中生成所有视频的视频缩略图会导致内存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35548761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com