1

In my app, I have a UIView which will drawrect according to user touch. It is meant to be used as a signature box. When the user is finished "drawing", they hit accept and the app continues. However, I would like to check if the UIView was actually drawn in, e.g. it isn't just blank. I tried making another empty UIImageView and checking to see if their image was equal but failed. Here is what I tried:

Note: signatureBox is the subclass of UIView which you can draw in. I save the signature, and compare it to the image in a blank UIImageView.

viewDidLoad{

    UIGraphicsBeginImageContext(self.signatureBox.bounds.size);
    [self.signatureBox.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData *imageData = UIImagePNGRepresentation(image);
    self.blankImage.image = [UIImage imageWithData:imageData];

}

mySaveMethod{
  UIGraphicsBeginImageContext(self.signatureBox.bounds.size);
    [self.signatureBox.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData *imageData = UIImagePNGRepresentation(image);
    self.signatureImage = [UIImage imageWithData:imageData];

    if(self.signatureImage == self.blankImage.image){
        NSLog(@"Blank");
    }
    else{
        NSLog(@"not blank");
    }

}

this nslogs "not blank" every time regardless of whether I draw in the the signatureBox or not. Any idea why?

Josue Espinosa
  • 5,009
  • 16
  • 47
  • 81
  • I don't think your comparison is very nice. Maybe this can help: [link][1] Edit: I'd like to point to what @rdelmar said, you never compare objects with =, objects use methods to be compared. [1]: http://stackoverflow.com/a/8182232/2902264 – Paroxysm Nov 21 '13 at 22:23
  • I don't know if this will work (I've never tried to compare images), but you don't compare objects with "=", you use isEqual: – rdelmar Nov 21 '13 at 22:23
  • if the suggestion above doesn't work, just check the color of each pixel in the image and see if it equal to the default color, if not then you know image is not blank. – Derick F Nov 21 '13 at 22:30

1 Answers1

1

Why not just have a BOOL that is set to YES as soon as the user first draws something in the box? This could be done in your gesture recognizer target method. If they hit save and the BOOL is still NO, they haven't drawn anything. Much simpler than attempting to analyze the resulting image.

Patrick Goley
  • 5,397
  • 22
  • 42