首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >正确初始化PHAssetCollection的探讨

正确初始化PHAssetCollection的探讨
EN

Stack Overflow用户
提问于 2015-05-20 17:22:08
回答 1查看 474关注 0票数 1

对于如何正确初始化相机应用程序中的PHAssetCollection,以便将视频保存到相册中,我有些困惑。我并没有试图对相册中的任何视频或图片做任何事情,我只是试图通过我的相机应用程序将捕获的视频保存到相册中,这个应用程序是围绕AVFoundation开发的。苹果在这方面的文档很少,我在这里看到的所有问题都没有完全解决这个问题。我是否应该在这个任务中使用一个资产集合?还是有一个更简单的方式使用Photos框架?

下面是在视频开始并完成录制时调用的委托函数

代码语言:javascript
复制
        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
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-21 02:24:36

我认为你对PHAssetCollection的所有谈论都是一只青鱼。因为您只想将视频保存到摄像机名册中,请拨打UISaveVideoAtPathToSavedPhotosAlbum

如果出于某些原因,您不愿意这样做,那么只需直接保存到照片库,大致如下(直接输入,没有测试,可能需要一些轻微的mods):

代码语言:javascript
复制
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
    PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(f)
    }, completionHandler: {
        (ok:Bool, err:NSError!) in
        // whatever
})
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30356380

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档