我正在尝试运行一个条件来测试图像是否是" silhouette.png"在使用imagePickerController用照相机拍摄的照片覆盖图像之前。
条件不起作用并导致我的应用程序崩溃,因为imageView1.image是"可选(大小{225,225}方向0缩放1.000000)"并根据我的println()值不断变化。我如何检查imageView1.image是否是silhouette.png(默认图像占位符)而不是相机中的新图像(newMedia == true)?
我正在做这个条件,因为我想在我的viewController上有多个imageViews,我希望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)
}
}
}
答案 0 :(得分:1)
您的条件无法正常工作,您正在比较同一图像的两个不同实例。它永远不会奏效。
另一种解决方案是比较这两种图像数据:
let data1 = UIImagePNGRepresentation(oldImage)
let data2 = UIImagePNGRepresentation(imageView1.image)
if data2.isEqualToData(data1) {
//Do your stuff
}