Swift - 从卷中的最后一张照片更新 UIButton 图像

标签 swift uibutton camera-roll

我正在尝试使用相机胶卷中的最后一张图像作为我的 UIButton 的背景图像。当 View 加载时,按钮会正确显示最后一张照片。但是当我拍照时,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()
                }
            })
        }
    }
}

我对整个编码工作还是陌生的,请提供任何帮助。谢谢你。

最佳答案

试试这个,我不确定你是否需要获取最后一张照片。只需像这样更新 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)
            })

        }
    })

关于Swift - 从卷中的最后一张照片更新 UIButton 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29897165/

相关文章:

objective-c - UIVideoAtPathIsCompatibleWithSavedPhotosAlbum 在 iPhone 上部署时返回 false

swift - UIScrollView 中的 UIImageView 和 UIWebView

ios - 替换代码中的 IBOutlet View

ios - 如何将UIButton的名称保存在字符串中?

ios - 在 ios 中录制视频时添加覆盖的方法

ios - 如何将相机胶卷中的所有照片和视频插入阵列?

ios - 在 UITableViewCell 中绘制一个彩色圆圈

ios - 修改 Popover View Controller 的宽度

iphone - 打乱 NSMutableArray 而不重复并显示在 UIButton 中

ios - 以编程方式创建的 UIButton 不可见