PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
options.version = PHImageRequestOptionsVersionCurrent;
//如果是选择icloud里面的视频,则要在options里打开允许下载的属性
[manager requestAVAssetForVideo:PHAsset options:videoRequestOptions resultHandler:^(AVAsset * avasset, AVAudioMix * audioMix, NSDictionary * info) {
AVURLAsset *urlAsset = (AVURLAsset *)asset;
NSURL *url = urlAsset.URL;
NSData *data = [NSData dataWithContentsOfURL:url];
//当在这个方法里获得的avasset为视频文件,但是有可能会发生这个错误
//-[AVComposition URL]: unrecognized selector sent to instance 0x138d17f40
//这是因为有些视频可能不存在这个url(比如慢视频)
//错误在AVURLAsset *urlAsset = (AVURLAsset *)asset;这一行代码。
//因为asset可能是个基类,他可能是AVComposition而不是AVURLAsset,这样就得不到视频的url了这样程序就会崩啦。
//<这里是解决办法1>
//options.version = PHVideoRequestOptionsVersionOriginal;
//但是这个方法有个缺点是如果是慢视频,则提交后播放就和正常视频一样了
//这是因为,默认的 version 属性是 PHVideoRequestOptionsVersionCurrent 这项将纳入慢动作、 用户编辑、 修剪等,更改这个属相就改变了视频的格式了。
//<所以完美解决的方法出来了>
//给定一个地址URL
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = paths.firstObject;NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"mergeSlowMoVideo-%d.mov",arc4random() % 1000]];
NSURL *url = [NSURL fileURLWithPath:myPathDocs];
//将这个地址设为视频的输出地址
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetHighestQuality];
exporter.outputURL = url;
exporter.outputFileType = AVFileTypeQuickTimeMovie;
exporter.shouldOptimizeForNetworkUse = YES;
[exporter exportAsynchronouslyWithCompletionHandler:^{dispatch_async(dispatch_get_main_queue(), ^{if (exporter.status == AVAssetExportSessionStatusCompleted) {
NSURL*URL = exporter.outputURL;
NSData *videoData = [NSData dataWithContentsOfURL:URL];
// Upload
[self uploadSelectedVideo:video data:videoData];
}});
}];
//注意排版可能根据你的代码来进行了,我这里将这几种写法都放在一起了,如果你要选择视频的话,请直接使用这个完美的解决方法。
}];
先说一下我的需页面 以9宫格的形式显示我手机相册里面的视频 ,我开始的方法是
每个视频都要获取到AVURLAsset再显示,要等的时间很长
[manager requestAVAssetForVideo:PHAsset options:videoRequestOptions resultHandler:^(AVAsset * avasset, AVAudioMix * audioMix, NSDictionary * info) {
AVURLAsset *urlAsset = (AVURLAsset *)asset;
NSURL *url = urlAsset.URL;
NSData *data = [NSData dataWithContentsOfURL:url];
可以先获取每个视频的缩略图
PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
options.version = PHVideoRequestOptionsVersionOriginal;
options.deliveryMode = PHVideoRequestOptionsDeliveryModeFastFormat;
options.networkAccessAllowed=true;
sellcount++;
PHAssetModel* model = [[PHAssetModelalloc]init];
model.asset= phAsset;
PHImageRequestOptions *option = [[PHImageRequestOptions alloc] init];
option.resizeMode = PHImageRequestOptionsResizeModeFast;
[[PHImageManager defaultManager] requestImageForAsset:phAsset targetSize:CGSizeMake(600, 600) contentMode:PHImageContentModeAspectFill options:option resultHandler:^(UIImage *result, NSDictionary *info) {
if(result) {
model.thumbnail=result;
[self.pictureArraddObject:model];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self->mainCollectionViewreloadData];
});
}];
这个很快,这样子显示列表页面不费时间;
这里要注意一点区分一下Download image from iCloud / 从iCloud下载图片
if([infoobjectForKey:PHImageResultIsInCloudKey]&&networkAccessAllowed ) {
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.progressHandler= ^(doubleprogress,NSError*error,BOOL*stop,NSDictionary*info) {
dispatch_async(dispatch_get_main_queue(), ^{
if(progressHandler) {
progressHandler(progress, error, stop, info);
}
});
};
options.networkAccessAllowed=YES;
options.resizeMode = PHImageRequestOptionsResizeModeFast;
[[PHImageManager defaultManager] requestImageDataForAsset:asset options:options resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
UIImage*resultImage = [UIImageimageWithData:imageData];
做一下判断去获取Image;
当点击collectionView didSelectItem时候 在加载
PHVideoRequestOptions* options = [[PHVideoRequestOptions alloc] init];
options.version = PHVideoRequestOptionsVersionOriginal;
options.deliveryMode = PHVideoRequestOptionsDeliveryModeAutomatic;
options.networkAccessAllowed = YES;
[[PHImageManager defaultManager] requestAVAssetForVideo:model.asset options:options resultHandler:^(AVAsset* avasset, AVAudioMix* audioMix, NSDictionary* info){
// NSLog(@"Info:\n%@",info);
AVURLAsset*videoAsset = (AVURLAsset*)avasset;
网友评论