我通过以下链接演示了如何将文件从Gallery复制到应用程序或其他目录(正确答案):
How to copy an image file from iOS Photo Library (ALAssetsLibrary) to the local directory of an App?
但是对于ALAssetsLibrary类文档,Apple表示现在已经弃用了iOS 9.0,而不是使用Photos框架。
从iOS 9.0开始,不推荐使用Assets Library框架。相反,使用 相反,它在iOS 8.0及更高版本中提供了更多 使用用户照片的功能和更好的性能 图书馆。有关更多信息,请参阅照片框架参考。
如何使用Photos框架将资源从Gallery复制到其他URL?
答案 0 :(得分:3)
这看起来相当容易。我为可能有帮助的人添加了一个示例代码:
var item: PHAsset! // you update with actual PHAsset at runtime
let docuPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.ApplicationDirectory, NSSearchPathDomainMask.UserDomainMask, true) as NSArray
let targetImgeURL = (docuPath[0] as! String) + "/IMG_0005.JPG"
let phManager = PHImageManager.defaultManager()
let options = PHImageRequestOptions()
options.synchronous = true; // do it if you want things running in background thread
phManager.requestImageDataForAsset(item, options: options)
{ imageData,dataUTI,orientation,info in
if let newData:NSData = imageData
{
try! newData.writeToFile(targetImgeURL, atomically: true)
}
}
答案 1 :(得分:0)
在Swift中使用下面的代码集
import Photos
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
self.dismissViewControllerAnimated(true, completion: nil)
if let referenceURL = info[UIImagePickerControllerReferenceURL] as? NSURL {
let fetchResult = PHAsset.fetchAssetsWithALAssetURLs([referenceURL], options: nil)
if let phAsset = fetchResult.firstObject as? PHAsset {
PHImageManager.defaultManager().requestAVAssetForVideo(phAsset, options: PHVideoRequestOptions(), resultHandler: { (asset, audioMix, info) -> Void in
if let asset = asset as? AVURLAsset {
let videoData = NSData(contentsOfURL: asset.URL)
// optionally, write the video to the temp directory
let videoPath = NSTemporaryDirectory() + "tmpMovie.MOV"
let videoURL = NSURL(fileURLWithPath: videoPath)
let writeResult = videoData?.writeToURL(videoURL, atomically: true)
if let writeResult = writeResult where writeResult {
print("success")
}
else {
print("failure")
}
}
})
}
}
}
答案 2 :(得分:0)
以下是目标C 解决方案。
-(NSURL*)createVideoCopyFromReferenceUrl:(NSURL*)inputUrlFromVideoPicker{
NSURL __block *videoURL;
PHFetchResult *phAssetFetchResult = [PHAsset fetchAssetsWithALAssetURLs:@[inputUrlFromVideoPicker ] options:nil];
PHAsset *phAsset = [phAssetFetchResult firstObject];
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
[[PHImageManager defaultManager] requestAVAssetForVideo:phAsset options:nil resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
if ([asset isKindOfClass:[AVURLAsset class]]) {
NSURL *url = [(AVURLAsset *)asset URL];
NSLog(@"Final URL %@",url);
NSData *videoData = [NSData dataWithContentsOfURL:url];
// optionally, write the video to the temp directory
NSString *videoPath = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%f.mp4",[NSDate timeIntervalSinceReferenceDate]]];
videoURL = [NSURL fileURLWithPath:videoPath];
BOOL writeResult = [videoData writeToURL:videoURL atomically:true];
if(writeResult) {
NSLog(@"video success");
}
else {
NSLog(@"video failure");
}
dispatch_group_leave(group);
// use URL to get file content
}
}];
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
return videoURL;
}