我试图运行一个条件,以测试图像是否为“ silhouette.png”,然后再使用imagePickerController从相机拍摄的图像覆盖图像。
该条件不起作用,导致我的应用程序崩溃,因为imageView1.image是“ Optional(size {225,225} orientation 0 scale 1.000000)(大小{225,225}方向),并且根据我的println()值不断变化。如何检查imageView1.image是否为silhouette.png(默认图像占位符),而不是来自相机的新图像(newMedia == true)?
我这样做是有条件的,因为我希望viewController上有多个imageView,并且希望imagePickerController在覆盖图像之前先对其进行检查。
谢谢。
func imagePickerController(picker:UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject:AnyObject]){
//what happens after the picture is chosen
let oldImage = UIImage(named:"silhouette.png")
println(imageView1.image)
//if conditional that isn't working
if imageView1.image == oldImage {
let mediaType = info[UIImagePickerControllerMediaType] as! NSString
self.dismissViewControllerAnimated(true, completion: nil)
if mediaType.isEqualToString(kUTTypeImage as NSString as String){
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
imageView1.image = image
if (newMedia == true){
UIImageWriteToSavedPhotosAlbum(image, self, "image:didFinishSavingWithError:contextInfo:", nil)
}
}
}
最佳答案
您的条件无效,您正在比较同一Image的两个不同实例。它永远都行不通。
另一种解决方案是像这样比较两个图像数据:
let data1 = UIImagePNGRepresentation(oldImage)
let data2 = UIImagePNGRepresentation(imageView1.image)
if data2.isEqualToData(data1) {
//Do your stuff
}