我对于如何正确初始化相机应用程序中的PHAssetCollection以便将视频保存到相册感到有些困惑。我不会尝试对相册中的任何视频或图片做任何事情,我只是想通过我围绕AVFoundation开发的相机应用程序将捕获的视频保存到相册中。 Apple的文档对此很少,我在这里看到的所有问题都无法完全解决。我是否应该为此任务使用资产收集?还是使用Photos框架有更简单的方法?
这是在视频开始和结束录制时调用的委托函数
println("capture output : finish recording to \(outputFileURL)")
self.weAreRecording = false
self.isSessionRunning = false
println("Getting moments in photo library")
let results: PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(PHAssetCollectionType.Moment, subtype: PHAssetCollectionSubtype.AlbumRegular, options: nil)
var assetCollection: PHAssetCollection?
// SHOULD I EVEN BE DOING THIS??? IM NOT TRYING TO DO ANYTHING IN THE PHOTOS ALBUM
// results.enumerateObjectsUsingBlock { (object, index, stop) -> Void in
//
// let assetCollection: PHAssetCollection = object as! PHAssetCollection
//
// momentsCount++
//
// blah blah blah
//
// }
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0), {
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
let request = PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(outputFileURL)
let assetPlaceholder = request.placeholderForCreatedAsset
// The problem is here, since I am not entirely sure how to go about initializing
// The assetCollection correctly
let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: assetCollection, assets: results)
albumChangeRequest.addAssets([assetPlaceholder])
}, completionHandler: {(success, error) in
if success {
NSLog("Adding video to Library -> %@", (success ? "Sucess" : "Error!"))
}
else {
println("There was an error saving the video")
}
})
})
}
func captureOutput(captureOutput: AVCaptureFileOutput!, didStartRecordingToOutputFileAtURL fileURL: NSURL!, fromConnections connections: [AnyObject]!) {
println("capture output: started recording to \(fileURL)")
self.isSessionRunning = true
self.weAreRecording = true
}
最佳答案
我认为您谈论的PHAssetCollection都是红色鲱鱼。由于您只想将视频保存到相机胶卷中,因此请调用UISaveVideoAtPathToSavedPhotosAlbum
。
如果由于某种原因您不愿意这样做,则可以直接将其保存到照片库中,大致像这样(直接键入,未经测试,可能需要一些修改):
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(f)
}, completionHandler: {
(ok:Bool, err:NSError!) in
// whatever
})
关于ios - 正确初始化PHAssetCollection,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30356380/