1

我正在尝试使用相机胶卷中的最后一张图像作为我的 UIButton 的背景图像。当视图加载时,按钮正确显示最后一张照片。但是当我拍照时,UIbutton 中的背景图像不会更新。这是我用来获取最后一张照片的代码

func lastPhoto() {
    let imgManager = PHImageManager.defaultManager()
    var fetchOptions = PHFetchOptions()
    fetchOptions.sortDescriptors = [NSSortDescriptor(key:"creationDate", ascending: true)]
    if let fetchResult = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image, options: fetchOptions) {
        imgManager.requestImageForAsset(fetchResult.lastObject as PHAsset, targetSize: self.view.frame.size, contentMode: PHImageContentMode.AspectFill, options: nil, resultHandler: { (image, _) in
            self.rollBtn.setBackgroundImage(image, forState: .Normal)
        })
    }
}

不确定它是否相关,但这是我用来拍照的代码片段

    switch (camMgr.cameraOutputMode) {
    case .StillImage:

        camMgr.capturePictureWithCompletition({ (image, error) -> Void in

            if let capturedImage = image? {
                println("capture image")

                //animate button effect
                UIView.animateWithDuration(0.3, animations: {
                    sender.backgroundColor = UIColor.greenColor()
                    }, completion: {(finished:Bool) in
                        println("inside")
                        sender.backgroundColor = UIColor.whiteColor()
                        self.lastPhoto()
                })
                UIView.animateWithDuration(0.1, animations: {
                    self.buttonView.backgroundColor = UIColor.blackColor()
                    }, completion: {(finished:Bool) in
                        println("inside")
                        self.buttonView.backgroundColor = UIColor.clearColor()
                        self.lastPhoto()
                })

            }
        })

    case .VideoWithMic, .VideoOnly:
        sender.selected = !sender.selected
        sender.backgroundColor = sender.selected ? UIColor.redColor() : UIColor.greenColor()
        if sender.selected {
            camMgr.startRecordingVideo()
        } else {
            camMgr.stopRecordingVideo({ (videoURL, error) -> Void in
                println(videoURL)
                if let errorOccured = error? {
                    UIAlertView(title: "Error occured", message: errorOccured.localizedDescription, delegate: nil, cancelButtonTitle: "OK").show()
                }
            })
        }
    }
}

我对这整个编码事情还是陌生的,请任何帮助都会很好。非常感谢你。

4

1 回答 1

1

试试这个,我不确定你需要获取最后一张照片。只需像这样更新 UIButton 背景图像。应该给你想要的效果。让我知道它是否修复了它。

       if let capturedImage = image? {
            println("capture image")

            //animate button effect
            UIView.animateWithDuration(0.3, animations: {
                sender.backgroundColor = UIColor.greenColor()
                }, completion: {(finished:Bool) in
                    println("inside")
                    sender.backgroundColor = UIColor.whiteColor()
            })
            UIView.animateWithDuration(0.1, animations: {
                self.buttonView.backgroundColor = UIColor.blackColor()
                }, completion: {(finished:Bool) in
                    println("inside")
                self.buttonView.backgroundColor = UIColor.clearColor()
               self.rollBtn.setBackgroundImage(capturedImage, forState: .Normal)
            })

        }
    })
于 2015-05-02T15:30:38.843 回答