将图像转换为CGBitmapContext

时间:2014-01-15 02:20:19

标签: ios objective-c image

我在目标C中相对较新,可以将png图像转换为CGBitmapContext。如果是,我该如何实现它。

2 个答案:

答案 0 :(得分:2)

虽然无法从UIImage获取像素值,但可以通过UIImage的CGImage访问它。请查看帖子中提供的this answer。 它使用onTouch方法在用户手指所在的特定x和y坐标处抓取RGB值。

答案 1 :(得分:1)

这是我找到的解决方案

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



}

}
}