简体   繁体   English

将图像转换为CGBitmapContext

[英]Convert an image to CGBitmapContext

I am relatively new in objective C, it is possible to convert a png image into a CGBitmapContext. 我在目标C中相对较新,可以将png图像转换为CGBitmapContext。 If it is, How I can implement it. 如果是这样,我将如何实施它。

While pixel values are not possible to obtain from a UIImage, it can be accessed through CGImage of the UIImage. 虽然无法从UIImage获取像素值,但可以通过UIImage的CGImage访问像素值。 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. 它使用onTouch方法在用户手指所在的特定x和y坐标处获取RGB值。

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



}

}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM