dblapps / DAScratchPad

MIT License
110 stars 24 forks source link

SCRATCH TOOL? #4

Open geogerar opened 10 years ago

geogerar commented 10 years ago

Hello, Very nice tool, thanks a lot for that. I will include it in one of my apps and when I am ready I will send you a notice. Only two notices! 1) How can I find out that my sketchView is empty....no drawing at all... 2)Can you implement an Eraser Tool also? Thanks again!

George N. Gerardis

dblapps commented 10 years ago

Hi George,

I ran into the same issue some time back with checking if a sketch is empty. Here's a link to a SO question with code I used (and I pasted the code below):

http://stackoverflow.com/questions/4735899/how-to-check-if-a-uiimage-is-blank-empty-transparent

I'll look into doing an eraser tool when I get some spare time.

Dave

BOOL isImageBlank(UIImage* image, UIColor* backgroundColor) { CGFloat r, g, b, a; [backgroundColor getRed:&r green:&g blue:&b alpha:&a]; uint8_t red = (uint8_t)(r * 255.0f); uint8_t green = (uint8_t)(g * 255.0f); uint8_t blue = (uint8_t)(b * 255.0f); uint8_t alpha = (uint8_t)(a * 255.0f);

typedef struct {
    uint8_t red;
    uint8_t green;
    uint8_t blue;
    uint8_t alpha;
} MyPixel_T;

CGImageRef cgImage = image.CGImage;

//Get a bitmap context for the image
CGContextRef bitmapContext = CGBitmapContextCreate(NULL, CGImageGetWidth(cgImage), CGImageGetHeight(cgImage),
                                                   CGImageGetBitsPerComponent(cgImage), CGImageGetBytesPerRow(cgImage),
                                                   CGImageGetColorSpace(cgImage), CGImageGetBitmapInfo(cgImage));

//Draw the image into the context
CGContextDrawImage(bitmapContext, CGRectMake(0, 0, CGImageGetWidth(cgImage), CGImageGetHeight(cgImage)), cgImage);

//Get pixel data for the image
MyPixel_T *pixels = CGBitmapContextGetData(bitmapContext);
size_t pixelCount = CGImageGetWidth(cgImage) * CGImageGetHeight(cgImage);

for (size_t i = 0; i < pixelCount; i++) {
    MyPixel_T p = pixels[i];
    //Your definition of what's blank may differ from mine
    if ((p.red != red) || (p.green != green) || (p.blue != blue) || (p.alpha != alpha)) {
        return NO;
    }
}

return YES;

}