Swift - 从最后一张照片更新 UIButton 图像
Swift - update UIButton image from last photo in roll
我正在尝试使用相机胶卷中的最后一张图片作为我的 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()
}
})
}
}
}
我对这整个编码事情还是陌生的,请提供任何帮助。谢谢你。
试试这个,我不确定您是否需要获取最后一张照片。只需像这样更新 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)
})
}
})
我正在尝试使用相机胶卷中的最后一张图片作为我的 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()
}
})
}
}
}
我对这整个编码事情还是陌生的,请提供任何帮助。谢谢你。
试试这个,我不确定您是否需要获取最后一张照片。只需像这样更新 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)
})
}
})