PHImageManager.default().requestAVAsset 用于视频
Posted
技术标签:
【中文标题】PHImageManager.default().requestAVAsset 用于视频【英文标题】:PHImageManager.default().requestAVAsset for video 【发布时间】:2016-09-28 22:41:00 【问题描述】:我的情况有很大的问题
我的主要问题是我需要一个接一个地获取视频对视频名称进行一些操作并将其保存到文件系统中,然后再次获取另一个视频对视频名称进行一些操作并通过循环将其保存到文件系统中资产问题是完成处理程序阻止我这样做,因为它将所有视频保存在一起而我没有进行任何编辑和更改名称这保存所有视频我认为它正在后台线程上工作请任何帮助解决这个问题我需要处理获取视频一个一个
这是我的代码
for asset in arrayOfAssets
if asset.mediaType == .video
PHImageManager.default().requestAVAsset(forVideo: asset, options: nil, resultHandler: (AVAsset, AVAudio, info) in
// i need access to this place so i can fetch the video one by one and working with the AVAsset
)
else
let imageOp = PHImageRequestOptions()
PHImageManager.default().requestImage(for: asset, targetSize: CGSize(width:125,height:125), contentMode: .aspectFit, options: imageOp, resultHandler: (img, info) in
print(img!)
)
【问题讨论】:
【参考方案1】:我认为这与您的查询有关 对于视频 How Can i Get the List of all Video files from Library in ios sdk
图片 Get all of the pictures from an iPhone photoLibrary in an array using AssetsLibrary framework?
allVideos = [[NSMutableArray alloc] init];
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
[assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop)
if (group)
[group setAssetsFilter:[ALAssetsFilter allVideos]];
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
if (asset)
dic = [[NSMutableDictionary alloc] init];
ALAssetRepresentation *defaultRepresentation = [asset defaultRepresentation];
NSString *uti = [defaultRepresentation UTI];
NSURL *videoURL = [[asset valueForProperty:ALAssetPropertyURLs] valueForKey:uti];
NSString *title = [NSString stringWithFormat:@"video %d", arc4random()%100];
UIImage *image = [self imageFromVideoURL:videoURL];
[dic setValue:image forKey:@"image"];
[dic setValue:title forKey:@"name"];
[dic setValue:videoURL forKey:@"url"];
[`allVideos` addObject:dic];
];
else
failureBlock:^(NSError *error)
NSLog(@"error enumerating AssetLibrary groups %@\n", error);
];
--------for ALL PHOTOS
NSArray *imageArray;
NSMutableArray *mutableArray;
-(void)getAllPhotosFromCamera
imageArray=[[NSArray alloc] init];
mutableArray =[[NSMutableArray alloc]init];
PHImageRequestOptions *requestOptions = [[PHImageRequestOptions alloc] init];
requestOptions.resizeMode = PHImageRequestOptionsResizeModeExact;
requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
requestOptions.synchronous = true;
PHFetchResult *result = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil];
NSLog(@"%d",(int)result.count);
PHImageManager *manager = [PHImageManager defaultManager];
NSMutableArray *images = [NSMutableArray arrayWithCapacity:[result count]];
// assets contains PHAsset objects.
__block UIImage *ima;
for (PHAsset *asset in result)
// Do something with the asset
[manager requestImageForAsset:asset
targetSize:PHImageManagerMaximumSize
contentMode:PHImageContentModeDefault
options:requestOptions
resultHandler:^void(UIImage *image, NSDictionary *info)
ima = image;
[images addObject:ima];
];
imageArray = [images copy]; // You can direct use NSMutuable Array images
【讨论】:
是的,Eric,但我们可以从中获得灵感。并且代码可以转换为swift。 objectivec2swift.com 但我不需要获取我的朋友需要在结果处理程序之间访问的所有视频 如果可以转成Swift,请自己动手。 // 就像我们不用 Python 答案来回答 Java 问题一样,一个 Swift 问题应该有 Swift 中的答案...以上是关于PHImageManager.default().requestAVAsset 用于视频的主要内容,如果未能解决你的问题,请参考以下文章