关于正确初始化PHAssetCollection

时间:2015-05-20 17:22:09

标签: ios swift ios8

我对如何正确初始化相机应用程序中的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
    }

1 个答案:

答案 0 :(得分:2)

我认为所有关于PHAssetCollection的讨论都是一个红色的鲱鱼。由于您只想将视频保存到相机胶卷中,请致电$Excel.ActiveSheet.UsedRange.Rows.Item($LastRow).Interior.ColorIndex = 11 $Excel.ActiveSheet.UsedRange.Rows.Item($LastRow).Font.ColorIndex = 6

如果由于某种原因您不愿意这样做,那么只需直接保存到照片库,大致类似于此(直接键入,未经测试,可能需要一些轻微的修改):

$ws.UsedRange