iOS 相机问题 (Swift)
iOS camera issue (Swift)
我正在尝试 运行 有条件地测试图像是否 "silhouette.png",然后它会使用 imagePickerController 使用从相机拍摄的照片覆盖图像。
条件不起作用并导致我的应用程序崩溃,因为 imageView1.image 是 "Optional( size {225, 225} orientation 0 scale 1.000000)" 并且根据我的 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)
}
}
}
您的条件不成立,您正在比较同一图像的两个不同实例。它永远不会起作用。
另一个解决方案是像这样比较两个图像数据:
let data1 = UIImagePNGRepresentation(oldImage)
let data2 = UIImagePNGRepresentation(imageView1.image)
if data2.isEqualToData(data1) {
//Do your stuff
}
我正在尝试 运行 有条件地测试图像是否 "silhouette.png",然后它会使用 imagePickerController 使用从相机拍摄的照片覆盖图像。
条件不起作用并导致我的应用程序崩溃,因为 imageView1.image 是 "Optional( size {225, 225} orientation 0 scale 1.000000)" 并且根据我的 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)
}
}
}
您的条件不成立,您正在比较同一图像的两个不同实例。它永远不会起作用。
另一个解决方案是像这样比较两个图像数据:
let data1 = UIImagePNGRepresentation(oldImage)
let data2 = UIImagePNGRepresentation(imageView1.image)
if data2.isEqualToData(data1) {
//Do your stuff
}