objective-c get pixel value from resized image

505 Views Asked by At

I have an issue with getting the value from a resized UIimage. The initial image size is 500x500. The method i'm using fails only when the image is resized (even to equal size)

  1. I'm using ImageContext and drawInRect to create new resized image.

  2. I'm using CFDataRef and CFDataGetBytePtr in another method to get pixel values at x,y.

CFDataRef returns @1000000 for all default images. Once the image is resized this value is changed to @90240000. Same with CFDataGetBytePtr which is empty after resizing the image.

Now i suspect it has something to do with the fact that resized image is actually a new image but i cannot be sure so i'd really appreciate any explanations or suggestions as to how i can resolve this.

Thank you for taking the time to check out my question.

1

There are 1 best solutions below

0
On

I've got a similar problem, that is I can't get the correct value using CFDataGetBytePtr after resizing the UIImage. I don't get the reason at present, but another way to get the value of UIImage works. Code as follow:

size_t width = CGImageGetWidth(img);
size_t height = CGImageGetHeight(img);


size_t rowByteSize = width *  4;
unsigned char * data = new unsigned char[height * rowByteSize];

CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(data, width, height, 8, rowByteSize,
                                             colorSpaceRef,
                                             kCGImageAlphaPremultipliedLast);
CGContextSetBlendMode(context, kCGBlendModeCopy);
CGContextDrawImage(context, CGRectMake(0.0, 0.0, width, height), img);
CGContextRelease(context);

CGColorSpaceRelease(colorSpaceRef);