AVURLAsset *urlAsset = (AVURLAsset *)asset崩潰

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為視頻文件,但是有可能會發(fā)生這個錯誤

//-[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];

}});

}];

//注意排版可能根據(jù)你的代碼來進行了,我這里將這幾種寫法都放在一起了,如果你要選擇視頻的話,請直接使用這個完美的解決方法。

}];

先說一下我的需頁面 以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];

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? });

? ? ? ? ? ? ? }];

這個很快,這樣子顯示列表頁面不費時間;


這里要注意一點區(qū)分一下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;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容