2012-04-23 4 views
0

나는 내가 RGBI를 설정하여 일부 픽셀 색상을 변경하고 싶습니다 UIImage 있습니다. 변경해야하는 모든 픽셀에 대한 CGPoint가 있습니다. RGBA 값을 설정하여 색상을 어떻게 바꿀 수 있습니까?컬러링 CGPoint UIImage의

UIImage 픽셀의 색상을 변경할 수 없다면 이미지가 포함 된 UIImageView를 그려도 괜찮습니다. 그러나 여러 번해볼 예정이므로 잘 수행해야합니다.

어떻게하면됩니까?

편집 : 나는 내 문제에 대한 해결책을 알아 냈

. 이 색상 CGPoint 레드 :

-(void)pixelate:(CGPoint)point 
{ 
    CGImageRef inImage = self.drawImage.image.CGImage; 
    CGContextRef cgctx = [self createARGBBitmapContextFromImage:inImage]; 
    if (cgctx == nil) { NSAssert(NO,@"Context shouldn't be nil!"); } 

    size_t w = CGImageGetWidth(inImage); 
    size_t h = CGImageGetHeight(inImage); 
    CGRect rect = {{0,0},{w,h}}; 

    CGContextDrawImage(cgctx, rect, inImage); 

    unsigned char* data = CGBitmapContextGetData(cgctx); 
    if (data != nil) 
    { 
     int offset = 4*((w*round(point.y))+round(point.x)); 
     data[offset] = 255; //alpha 
     data[offset+1] = 255; //red 
     data[offset+2] = 0; //green 
     data[offset+3] = 0; //blue 
    } 

    CGImageRef img = CGBitmapContextCreateImage(cgctx); 
    UIGraphicsEndImageContext(); 

    CGContextRelease(cgctx); 

    if (data) { free(data); } 

    self.drawImage.image = [UIImage imageWithCGImage:img]; 
} 
+2

드로잉 성능은 액세스 패턴에 크게 의존합니다. 픽셀을 한 번에 하나씩 설정하는 것은 일반적으로 그리 효율적이지 않습니다. 당신이 높은 수준에서 무엇을하고 있는지를 아는 것이 도움이 될 것입니다. –

+0

나는 페인팅 프로그램에서 "양동이"도구를 만들려고합니다. 나는 픽셀 단위로 색칠하는 재귀 적 방법이 그것을 할 것이라고 생각했다. 나는 더 나은 해결책에 대해 듣게되어 기쁩니다. –

답변

0

도움이 될 것입니다. 이것은 CGPoint 빨강을 나타냅니다.

-(void)pixelate:(CGPoint)point 
{ 
    CGImageRef inImage = self.drawImage.image.CGImage; 
    CGContextRef cgctx = [self createARGBBitmapContextFromImage:inImage]; 
    if (cgctx == nil) { NSAssert(NO,@"Context shouldn't be nil!"); } 

    size_t w = CGImageGetWidth(inImage); 
    size_t h = CGImageGetHeight(inImage); 
    CGRect rect = {{0,0},{w,h}}; 

    CGContextDrawImage(cgctx, rect, inImage); 

    unsigned char* data = CGBitmapContextGetData(cgctx); 
    if (data != nil) 
    { 
     int offset = 4*((w*round(point.y))+round(point.x)); 
     data[offset] = 255; //alpha 
     data[offset+1] = 255; //red 
     data[offset+2] = 0; //green 
     data[offset+3] = 0; //blue 
    } 

    CGImageRef img = CGBitmapContextCreateImage(cgctx); 
    UIGraphicsEndImageContext(); 

    CGContextRelease(cgctx); 

    if (data) { free(data); } 

    self.drawImage.image = [UIImage imageWithCGImage:img]; 
} 
0

우선 Imaport 다음 파일 .. >>>> 편집 ....

#import <QuartzCore/QuartzCore.h> 

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
CGContextRef bitmContext = CGBitmapContextCreate(NULL, width, height, 8,bytesPerRow,    colorSpace, kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst); 
CGColorSpaceRelease(colorSpace); 
CGContextDrawImage(bitmContext, (CGRect){.origin.x = 0.0f, .origin.y = 0.0f, .size.width =  originalWidth, .size.height = originalHeight}, cgImage); 

잡아 기본 화소에 대한 참조

UInt8* data = (UInt8*)CGBitmapContextGetData(bitmContext); 

픽셀 작동 :

const size_t bitmapByteCount = bytesPerRow * originalHeight; 
for (size_t i = 0; i < bitmapByteCount; i += 4) 
{ 
UInt8 a = data[i]; 
UInt8 r = data[i + 1]; 
UInt8 g = data[i + 2]; 
UInt8 b = data[i + 3]; 

data[i] = (UInt8)newAlpha 
data[i + 1] = (UInt8)newRed; 
data[i + 2] = (UInt8)newGreen; 
data[i + 3] = (UInt8)newBlue; 
} 

CGImageRef newImage = CGBitmapContextCreateImage(bitmContext); 

또는이에 refrence를 사용하는 10 ...

http://www.markj.net/iphone-uiimage-pixel-color/

희망, 이것은 내 문제에 대한 해결책을 알아 냈 you..enjoy

+0

고맙습니다. 절대로 실제로 솔루션을 사용할 수 없지만 올바른 솔루션을 찾게했습니다. 고맙습니다. –

+0

참조를 참조하십시오 .. 도움이 될 수 있습니다 ... 그리고 당신은 ViewController에서 를 가져와야합니다 – Nit