【问题标题】:swift - saving picture in picture app [duplicate]swift - 在图片应用程序中保存图片[重复]
【发布时间】:2016-07-03 04:50:17
【问题描述】:

我制作了一个用户拍照的 iPhone 应用。我需要在我的代码中添加什么,以便用户在拍照时将照片保存到用户库中?

以下是我目前的代码

导入 UIKit 导入 AVFoundation

类视图控制器:UIViewController、UIImagePickerControllerDelegate、UINavigationControllerDelegate {

var picker = UIImagePickerController()

@IBOutlet var camera: UIButton!



@IBAction func camera(sender: UIButton) {


        if UIImagePickerController.availableCaptureModesForCameraDevice(.Rear) != nil {
            picker = UIImagePickerController() //make a clean controller
            picker.allowsEditing = false
            picker.sourceType = UIImagePickerControllerSourceType.Camera
            picker.cameraCaptureMode = .Photo
            picker.showsCameraControls = true

            //customView stuff
            let customViewController = CustomOverlayViewController(
                nibName:"CustomOverlayViewController",
                bundle: nil
            )


            let customView:CustomOverlayView = customViewController.view as! CustomOverlayView
            customView.frame = self.picker.view.frame

            customView.cameraLabel.text = "Hello Cute Camera"


            picker.modalPresentationStyle = .FullScreen
            presentViewController(picker,animated: true,completion: {
                    self.picker.cameraOverlayView = customView
                }
            )




        } else { //no camera found -- alert the user.
            let alertVC = UIAlertController(
                title: "No Camera",
                message: "Sorry, this device has no camera",
                preferredStyle: .Alert)
            let okAction = UIAlertAction(
                title: "OK",
                style:.Default,
                handler: nil)
            alertVC.addAction(okAction)
            presentViewController(
                alertVC,
                animated: true,
                completion: nil)
        }


    //MARK: Picker Delegates
    func imagePickerController(
        picker: UIImagePickerController,
        didFinishPickingMediaWithInfo info: [String : AnyObject])
    {
        let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage //2
        UIImageWriteToSavedPhotosAlbum(chosenImage, self,nil, nil)
    }
    //What to do if the image picker cancels.
    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        dismissViewControllerAnimated(true,
            completion: nil)
    }




    }






override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

【问题讨论】:

    标签: swift uiimagepickercontroller


    【解决方案1】:

    这是我在我的应用程序上所做的,这可能会有所帮助。

    func saveImage(image: UIImage, funParam: (Bool) -> ()) ->Bool { //, metadata: NSDictionary) {
            if assetCollection == nil {
                return false                         // if there was an error upstream, skip the save
            }
    
            //self.validateStatus()
    
        //TODO: the return false is happening before the photo is save.
            PHPhotoLibrary.sharedPhotoLibrary().performChanges({
                let assetChangeRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(image)
                let assetPlaceHolder = assetChangeRequest.placeholderForCreatedAsset
                let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: self.assetCollection)
                albumChangeRequest!.addAssets([assetPlaceHolder!])                                                      
                }, completionHandler: { (success, error) -> Void in
                    if success {
                        funParam(true)
                    }
                    else {
                        funParam(false)
                    }
    
            })
            return true
    
    }
    

    参数如下:

    1. 图像是您要保护的图像。

    2. funParam 是一个函数,如果您想同步控制图像的保存,它将作为完成处理程序。我这样做 因为实际保存图片的代码是异步运行的。

    带有通话示例的更新消息:

    这是我如何调用此代码的示例:

    我有一个名为 PhotoAlbum.swif 的类,我像这样调用该函数:

    let validate = self.saveImage(imageView.image!, funParam: getSaveResponse)
    

    getSaveResponse 是一种控制图像安全后应该发生什么的方法。

    【讨论】:

    • 我会将这段代码放在我的代码中的什么位置?
    • 如果你想执行我推荐的代码,你可以有一个按钮,或者你可以将它添加为你已经拥有的 imagePickerController func (didFinishPickingMediaWithInfo) 的一部分。如果此答案满足您的问题,请将问题标记为已回答
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多