对于如何正确初始化相机应用程序中的PHAssetCollection,以便将视频保存到相册中,我有些困惑。我并没有试图对相册中的任何视频或图片做任何事情,我只是试图通过我的相机应用程序将捕获的视频保存到相册中,这个应用程序是围绕AVFoundation开发的。苹果在这方面的文档很少,我在这里看到的所有问题都没有完全解决这个问题。我是否应该在这个任务中使用一个资产集合?还是有一个更简单的方式使用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
}发布于 2015-05-21 02:24:36
我认为你对PHAssetCollection的所有谈论都是一只青鱼。因为您只想将视频保存到摄像机名册中,请拨打UISaveVideoAtPathToSavedPhotosAlbum。
如果出于某些原因,您不愿意这样做,那么只需直接保存到照片库,大致如下(直接输入,没有测试,可能需要一些轻微的mods):
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(f)
}, completionHandler: {
(ok:Bool, err:NSError!) in
// whatever
})https://stackoverflow.com/questions/30356380
复制相似问题