// 照片选择回调
[_tzImagePickerVCsetDidFinishPickingPhotosWithInfosHandle:^(NSArray *photos,NSArray*assets,BOOLisSelectOriginalPhoto,NSArray *infos) {
if(assets.count>0) {
[[PHImageManagerdefaultManager]requestImageDataForAsset:assets[0]options:nilresultHandler:^(NSData*_NullableimageData,NSString*_NullabledataUTI,UIImageOrientationorientation,NSDictionary*_Nullableinfo) {
NSURL*url = [infovalueForKey:@"PHImageFileURLKey"];
NSString*str = url.absoluteString;
NSArray*arr = [strcomponentsSeparatedByString:@"/"];
NSString*imgName = [arrlastObject]; //图片名称
NSNumber*size;
[urlgetResourceValue:&sizeforKey:NSURLFileSizeKeyerror:nil];
NSLog(@"size is %.2fM",[sizefloatValue]/(1024.0*1024.0)); //图片大小
[[QCALiYunManager shareManager]uploadWithFileURL:url fileName:imgName];
}];
}
}];
//视频选择回调
//这种方法是ios9.0之后才有的,所以不兼容9.0以下的版本
[_tzImagePickerVCsetDidFinishPickingVideoHandle:^(UIImage*coverImage,PHAsset*asset) {
STRONGSELF
if(@available(iOS9.0, *)) {
NSArray*assetResources = [PHAssetResourceassetResourcesForAsset:asset];
PHAssetResource*resource;
for(PHAssetResource*assetResinassetResources) {
if(assetRes.type==PHAssetResourceTypePairedVideo||
assetRes.type==PHAssetResourceTypeVideo) {
resource = assetRes;
}
}
NSString* fileName =@"";
if(resource.originalFilename) {
fileName = resource.originalFilename;
}
if (asset.mediaType == PHAssetMediaTypeVideo || asset.mediaSubtypes == PHAssetMediaSubtypePhotoLive)
{
PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
options.version = PHImageRequestOptionsVersionCurrent;
options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
//创建保存到沙盒的路径
NSString*uuidString = [[NSUUIDUUID]UUIDString];
NSString*tempFileName = uuidString;
NSString *documentPath = [NSString stringWithFormat:@"%@/photosFile", [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]];
NSFileManager*fileManager = [NSFileManagerdefaultManager];
BOOLisDir=NO;
BOOLexisted = [fileManagerfileExistsAtPath:documentPathisDirectory:&isDir];
if(!(existed&&isDir)){
[fileManagercreateDirectoryAtPath:documentPath withIntermediateDirectories:YES attributes:nil error:nil];
}
NSString*filePath=[documentPathstringByAppendingPathComponent:tempFileName];
[[PHAssetResourceManager defaultManager]writeDataForAssetResource:resource toFile:[NSURL fileURLWithPath:filePath] options:nil completionHandler:^(NSError * _Nullable error) {
if(error==nil) {
[[QCALiYunManagershareManager]uploadWithFileURL:[NSURLfileURLWithPath:filePath]fileName:fileName];
}else{
[strongSelffailResponseWithRequest:requestcode:ENUM_ResolveFailmessage:@"视频获取失败"];
}
}];
}
}else{
[strongSelffailResponseWithRequest:request code:ENUM_ResolveFail message:@"系统版本过低,请升级系统版本后再上传!"];
}
}];
//这种方法获取不到慢动作视频的文件地址,即AVAsset为AVComposition时获取不到文件地址,待研究
[_tzImagePickerVCsetDidFinishPickingVideoHandle:^(UIImage*coverImage,PHAsset*asset) {
PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
options.version = PHImageRequestOptionsVersionCurrent;
options.deliveryMode = PHVideoRequestOptionsDeliveryModeAutomatic;
[[PHImageManager defaultManager] requestAVAssetForVideo:asset options:options resultHandler:^(AVAsset *_Nullableasset, AVAudioMix *_NullableaudioMix, NSDictionary *_Nullableinfo) {
if(asset && [asset isKindOfClass:[AVURLAsset class]]) {
AVURLAsset *urlAsset = (AVURLAsset *)asset;
NSURL *url = urlAsset.URL;
NSString *str = url.absoluteString;
NSArray *arr = [str componentsSeparatedByString:@"/"];
NSString *imgName = [arr lastObject];
NSNumber *size;
[url getResourceValue:&size forKey:NSURLFileSizeKey error:nil];
NSLog(@"size is %.2fM",[size floatValue]/(1024.0*1024.0));
[[QCALiYunManager shareManager]uploadWithFileURL:url fileName:imgName];
}elseif([asset isKindOfClass:[AVComposition class]]) {
}
}];
}];
参考:
https://www.jianshu.com/p/2af1c0365ee4