0

I have 2 UIImageViews connected to an array of images I'm trying compare both once they are displayed but doesn't seems to work. I tried using imageArray[Image Literal] and also imageArray[image1.png, image2.png, image3.png, image4.png, image5.png] I'm not sure what im doing wrong. im not looking for the code although it may help but what im looking is for a someone to guide me to the right direction

@IBOutlet weak var 1ImageView: UIImageView!
@IBOutlet weak var 2ImageView: UIImageView!

    let imageArray = [image1.png, image2.png, image3.png, image4.png, image5.png]

func any() {
    if (1ImageView != nil) && (2ImageView != nil) && isEqual(image1.png) {
        print("match!")
    } else if ...// more if statements
    …last if statement} else {
        print(“no match!”)
}

@IBAction func buttonPressed(_ sender: IUButton) {
    any()
}

If this is not possible is there a way to assign an identifier to each of the images inside the array.. sorry for the extra question. there is one answer on comparing 2 images using NSData but Im not sure how to implement it to an array. thanks and sorry but the newbie question.

Ty Watson
  • 13
  • 1
  • 7
  • Question is a little unclear. What's the goal of the comparison? Also if you know how to compare two images, then you should have no problems in finding an image within an array (there are lots of posts here on SO that deal with searches through arrays/collections). – Cristik Feb 22 '19 at 05:25
  • i'm trying to figure how to compare multiple images inside an array. nowhere in my post said that I know, that's there reason I'm asking. and yes I checked probably more 100 post and most are for other languages or for obj-c or for java. also even you have replies in those question without helping just the same type of response you just post here. – Ty Watson Feb 22 '19 at 05:52
  • `there is one answer on comparing 2 images using NSData ` - I assumed since you know about that answer, you'd know how to compare two images. And once you know how to compare two images, searching through an array of images should not be complicated. – Cristik Feb 22 '19 at 06:50
  • Follow this [Link](https://stackoverflow.com/questions/11342897/how-to-compare-two-uiimage-objects) and I hope this will resolve your issue. – Ipy Creator Feb 22 '19 at 09:56

1 Answers1

0

image.isEqual(image) seems to be unreliable, despite what documentation says. If you don't need to make a pixel perfect comparison, converting image to a data and comparing those would be sufficient.

    let image1 = UIImage(named: "image1.png")
    let image2 = UIImage(named: "image2.png")

    let imageData1 = image1?.pngData()
    let imageData2 = image2?.pngData()

    if imageData1 == imageData2 {
        print("images are the same")
    } else {
        print("images are different")
    }

Looking for a specific image inside an array can build on that:

// array of images referencing image files within an Xcode project
// it's not the best idea to force unwrap those, but for the sake of simplicity
let imageArray = [UIImage(named: "image1.png")!,
                  UIImage(named: "image2.png")!,
                  UIImage(named: "image3.png")!,
                  UIImage(named: "image4.png")!,
                  UIImage(named: "image5.png")!]

func anySimilarImages() {
    // find first image which is the same as 1ImageView's
    let 1ImageViewImage: UIImage? = imageArray.first { (image) -> Bool in
        return  image.pngData() == 1ImageView.image?.pngData()
    }

    // find first image which is the same as 2ImageView's
    let 1ImageViewImage: UIImage? = imageArray.first { (image) -> Bool in
        return  image.pngData() == 2ImageView.image?.pngData()
    }

    if 1ImageViewImage != nil && 2ImageViewImage != nil {
        print("both images were found")
    }
    else if 1ImageViewImage != nil {
        print("first image was found")
    }
    else if 2ImageViewImage != nil {
        print("second image was found")
    }
    else {
        print("no image was found")
    }
}
xius
  • 596
  • 1
  • 6
  • 20