I am relatively new in objective C, it is possible to convert a png image into a CGBitmapContext. If it is, How I can implement it.
Asked
Active
Viewed 588 times
0
-
2What are you really trying to do? Why do you think you need a CGBitmapContext? If you just want to draw, why not simply draw the image into a graphics context? – matt Jan 15 '14 at 02:39
-
Yes, are you certain you need to use a `CGBitmapContext`? It's a low-level API; very powerful, but complex. Consider starting with `UIImage` and see if that does what you need. It can decode PNG files for you, and much more. – gavinb Jan 15 '14 at 03:00
-
Hi Matt, My intention it is to obtain the pixel data RGB value from it through a touch or a pan. – David Moya Jan 15 '14 at 03:02
-
Any other simpler way to go? – David Moya Jan 15 '14 at 03:03
-
possible duplicate of [How to get pixel data from a UIImage (Cocoa Touch) or CGImage (Core Graphics)?](http://stackoverflow.com/questions/448125/how-to-get-pixel-data-from-a-uiimage-cocoa-touch-or-cgimage-core-graphics) – rob mayoff Jan 15 '14 at 04:41
-
2I don't mean to criticise (okay, yes I do, actually), but if that's what you want to know then that's what you should ask. You still really aren't stating clearly what question you want the answer to. Perhaps what you want to know is whether the pixel the user has tapped on is transparent; in that case, [see my answer here](http://stackoverflow.com/questions/1042830/retrieving-a-pixel-alpha-value-for-a-uiimage/3763313#3763313). (And if you need fuller RGB info on a pixel, it's easy to see how to adapt that code.) – matt Jan 15 '14 at 04:50
2 Answers
2
While pixel values are not possible to obtain from a UIImage, it can be accessed through CGImage of the UIImage. Have a look at this answer provided in the post.
It uses the onTouch
method to grab the RGB value at a particular x and y coordinate that the user's finger is on.
1
That is the solution I found
- (IBAction)handlePans:(UIPanGestureRecognizer *)event{
if (event.numberOfTouches==1){
tranx = [event locationInView:self].x;
trany = [event locationInView:self].y;
CGPoint vel = [event velocityInView:self];
velocityx = vel.x;
velocityy = vel.y;
// NSLog(@"x: %d y:%d", tranx,trany);
//NSLog(@"velocity x - %f", vel.x);
//NSLog(@"velocity y - %f", vel.y);
_imageselct = [UIImage imageNamed:@"GASOLINE.png"];
CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(_imageselct.CGImage));
const UInt8* data = CFDataGetBytePtr(pixelData);
int pixelInfo = ((workingImage.size.width * trany) + tranx ) * 4;
float red = data[pixelInfo];
float green = data[(pixelInfo+1)];
float blue = data[pixelInfo + 2];
float alpha = data[pixelInfo + 3];
CFRelease(pixelData);
UIColor* color = [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:alpha/255.0f];
if ([color getRed:&red green: &green blue: &blue alpha: &alpha]){
NSLog(@"r: %f g: %f b: %f a: %f", red, green, blue, alpha);
}
}
}

David Moya
- 107
- 1
- 2
- 8
-
Performance note: CGDataProviderCopyData copies the entire image. I'm not an expert, but your original instinct to use CGBitmapContext might have been more efficient. Specifically, use CGBitmapContextGetData to get a *pointer* (rather than a copy). See http://stackoverflow.com/a/12579413/199364 – ToolmakerSteve Jan 25 '17 at 17:33