實(shí)況圖片其實(shí)就是一張封面圖和一個(gè).mov視頻陡舅,它們是通過一個(gè)identifier來關(guān)聯(lián)起來的陨囊。
/// 給圖片文件寫入live信息
/// @param photoURL 圖片地址
/// @param outputFile 輸出地址
/// @param identifier identifier => [[NSUUID UUID] UUIDString]
+ (BOOL)writeMetaDataByPath:(NSURL*)photoURL toPath:(NSString *)outputFile identify:(NSString *)identifier {
NSMutableData *data = [NSData dataWithContentsOfURL:photoURL].mutableCopy;
UIImage *image = [UIImage imageWithData:data];
CGImageRef imageRef = image.CGImage;
NSDictionary *imageMetadata = @{(NSString *)kCGImagePropertyMakerAppleDictionary : @{@"17" : identifier}};
CGImageDestinationRef dest = CGImageDestinationCreateWithData((CFMutableDataRef)data, kUTTypeJPEG, 1, nil);
CGImageDestinationAddImage(dest, imageRef, (CFDictionaryRef)imageMetadata);
CGImageDestinationFinalize(dest);
return [data writeToFile:outputFile atomically:YES];
}
// 成功block
typedef void(^VideoCompositionSuccess)(NSString *filePath);
// 失敗block
typedef void(^VideoCompositionFail)(NSString *errorMsg);
/// 視頻MP4轉(zhuǎn)Mov顽频,并寫入identifier信息
/// @param sourcePath 視頻源文件地址
/// @param outputPath 輸出地址
/// @param identifier identifier => [[NSUUID UUID] UUIDString]
/// @param success 成功回調(diào)
/// @param fail 失敗回調(diào)
- (void)mp4FileTransformToMovWithSourcePath:(NSString *)sourcePath outputPath:(NSString *)outputPath identifier:(NSString *)identifier success:(VideoCompositionSuccess)success fail:(VideoCompositionFail)fail {
NSURL *sourceUrl = [NSURL fileURLWithPath:sourcePath];
AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:sourceUrl options:nil];
NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset];
if ([compatiblePresets containsObject:AVAssetExportPresetHighestQuality]) {
_exportSession = [[AVAssetExportSession alloc] initWithAsset:avAsset presetName:AVAssetExportPresetHighestQuality];
AVMutableMetadataItem* item = [AVMutableMetadataItem metadataItem];
item.keySpace = AVMetadataKeySpaceQuickTimeMetadata;
item.key = AVMetadataQuickTimeMetadataKeyContentIdentifier;
item.value = identifier;
NSArray* metadata = [NSArray arrayWithObject:item];
_exportSession.metadata = metadata;
_exportSession.outputURL = [NSURL fileURLWithPath:outputPath];
_exportSession.outputFileType = @"com.apple.quicktime-movie";//AVFileTypeQuickTimeMovie;
_exportSession.shouldOptimizeForNetworkUse = YES;
//如有此文件則直接返回
if ([[NSFileManager defaultManager] fileExistsAtPath:outputPath]) {
if (success) {
success(outputPath);
}
return;
}
__block AVAssetExportSession *weakSession = _exportSession;
[_exportSession exportAsynchronouslyWithCompletionHandler:^(void) {
switch (weakSession.status) {
case AVAssetExportSessionStatusUnknown: {
break;
}
case AVAssetExportSessionStatusWaiting: {
break;
}
case AVAssetExportSessionStatusExporting: {
break;
}
case AVAssetExportSessionStatusCompleted: {
NSLog(@"mov file size:%lf MB",[NSData dataWithContentsOfURL:weakSession.outputURL].length/1024.f/1024.f);
NSData *da = [NSData dataWithContentsOfFile:outputPath];
NSLog(@"da:%lu",(unsigned long)da.length);
if (success) {
success(outputPath);
}
}
break;
case AVAssetExportSessionStatusFailed: {
if (fail) {
fail(@"視頻格式轉(zhuǎn)換出錯(cuò)");
}
}
break;
case AVAssetExportSessionStatusCancelled: {
if (fail) {
fail(weakSession.error.localizedDescription);
}
}
break;
}
}];
}
}
/// 把有l(wèi)ive信息的圖片和.mov視頻保存到相冊(cè)
#import <Photos/Photos.h>
NSURL *photo = [NSURL fileURLWithPath:finalCoverPath];
NSURL *video = [NSURL fileURLWithPath:finalVideoPath];
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetCreationRequest *request = [PHAssetCreationRequest creationRequestForAsset];
[request addResourceWithType:PHAssetResourceTypePhoto fileURL:photo options:nil];
[request addResourceWithType:PHAssetResourceTypePairedVideo fileURL:video options:nil];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (success) {
dispatch_async(dispatch_get_main_queue(), ^{
[BaseProgressHUD showMiddleInfoText:@"已保存到相冊(cè)" inView:nil];
});
}else {
dispatch_async(dispatch_get_main_queue(), ^{
[BaseProgressHUD showMiddleInfoText:@"保存失敗毙沾,請(qǐng)重試" inView:nil];
});
}}];