第一:将相册图片写入到沙盒
首先获取相册中要保存的图片:UIImage *image;
怎么获取图片在这我就不详细说明了。
//logpath:沙盒路径
NSString * logPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingString:@"xxx"];
//将图片写入沙盒
[UIImagePNGRepresentation(image) writeToFile:logPath atomically:YES];
第二:将视频导入到沙盒
视频导入到沙盒,那不能像图片那样直接写入到沙盒。解析一下,为什么视频不像图片一样一次性开辟本身大小的内存写入?想想,如果1个视频有1G多,难道直接开辟1G多的空间大小来写?所以,我们要将原始视频的URL转化为NSData数据,写入沙盒。
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *url = @“视频的URL”;
if (url) {
[assetLibrary assetForURL:url resultBlock:^(ALAsset *asset) {
ALAssetRepresentation *rep = [asset defaultRepresentation];
NSString videoPath = @“要保存的沙盒路径”;
char const cvideoPath = [videoPath UTF8String];
FILE file = fopen(cvideoPath, "a+");
if (file) {
const int bufferSize = 1024 * 1024;
// 初始化一个1M的buffer
Byte buffer = (Byte)malloc(bufferSize);
NSUInteger read = 0, offset = 0, written = 0;
NSError err = nil;
if (rep.size != 0)
{
do {
read = [rep getBytes:buffer fromOffset:offset length:bufferSize error:&err];
written = fwrite(buffer, sizeof(char), read, file);
offset += read;
dispatch_async(dispatch_get_main_queue(), ^{
progressIndicator.percentage = (float)offset/rep.size;
progressIndicator.centerLabel.text = [NSString stringWithFormat:@"%ld:%lld",offset/(10241024),rep.size/(10241024)];
});
} while (read != 0 && !err);//没到结尾,没出错,ok继续
}
// 释放缓冲区,关闭文件
free(buffer);
buffer = NULL;
fclose(file);
file = NULL;
dispatch_async(dispatch_get_main_queue(), ^{
[self videoWithIndex:index + 1 withFileName:self.currentdir];
});
}
} failureBlock:nil];
}
});
第三:导出音乐到沙盒
AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:@“音乐的url” options:nil];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"locals"];
NSLog (@"compatible presets for songAsset: %@",[AVAssetExportSession exportPresetsCompatibleWithAsset:songAsset]);
NSArray *ar = [AVAssetExportSession exportPresetsCompatibleWithAsset: songAsset];
NSLog(@"%@", ar);
AVAssetExportSession *exporter = [[AVAssetExportSession alloc]
initWithAsset: songAsset
presetName: AVAssetExportPresetAppleM4A];
NSLog (@"created exporter. supportedFileTypes: %@", exporter.supportedFileTypes);
exporter.outputFileType = @"com.apple.m4a-audio";
NSString *exportFile = [documentsDirectoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.m4a",@“xxx”]];
}
NSError error1;
if([fileManager fileExistsAtPath:exportFile])
{
[fileManager removeItemAtPath:exportFile error:&error1];
}
NSURL exportURL = [NSURL fileURLWithPath:exportFile];
exporter.outputURL = exportURL;
// do the export
[exporter exportAsynchronouslyWithCompletionHandler:^
{
// NSData *data1 = [NSData dataWithContentsOfFile:exportFile];
//NSLog(@"==================data1:%@",data1);
int exportStatus = exporter.status;
switch (exportStatus) {
case AVAssetExportSessionStatusFailed: {
// log error to text view
NSError *exportError = exporter.error;
NSLog (@"AVAssetExportSessionStatusFailed: %@", exportError);
break;
}
case AVAssetExportSessionStatusCompleted: {
NSLog (@"AVAssetExportSessionStatusCompleted");
break;
}
case AVAssetExportSessionStatusUnknown: {
NSLog (@"AVAssetExportSessionStatusUnknown");
break;
}
case AVAssetExportSessionStatusExporting: {
NSLog (@"AVAssetExportSessionStatusExporting");
break;
}
case AVAssetExportSessionStatusCancelled: {
NSLog (@"AVAssetExportSessionStatusCancelled");
break;
}
case AVAssetExportSessionStatusWaiting: {
NSLog (@"AVAssetExportSessionStatusWaiting");
break;
}
default:
{ NSLog (@"didn't get export status");
break;
}
}
}];
大功告成!有什么疑问或者错误,欢迎留言。