0

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
msavic
  • 1
  • 1
  • can you post the code where you resize the image? it might give us some hints – Raica Dumitru Cristian Dec 08 '15 at 15:32
  • Sure :) Here it is: // resize images + (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize { //UIGraphicsBeginImageContext(newSize); UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0); [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } – msavic Dec 09 '15 at 10:23
  • you could try this category and see if it improves something http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/ . Source :http://stackoverflow.com/questions/2658738/the-simplest-way-to-resize-an-uiimage. Also , are you doing this on main thread? – Raica Dumitru Cristian Dec 10 '15 at 10:26
  • Thank you. I've read the article and none of the methods or techniques described therein helped me understand the nature of my problem. Since my app is in the test phase i have but a few methods and they are all still located in one class. No additional threads are initialized at this point. – msavic Dec 10 '15 at 12:17

1 Answers1

0

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);