2012-04-29 2 views
0

CGContextDrawImage가 상당히 비쌀 수 있으므로 픽셀 데이터를 검사하는 동안 데이터 양을 최소화하려고합니다. 두 개의 이미지와 교차로의 CGRect가있는 경우 CGContextDrawImage를 각각 독립적으로 그릴 수 있습니까 (결과적으로 이미지 중 하나의 교차가 포함 된 두 개의 CGContextRef가 생성됩니다). 여기 두 이미지의 교차 만 포함하는 CGContextDrawImage

당신이 정말 CGBitmapContext를 원하지 않는다면 당신이 그들을 그릴 필요가 없습니다, 음 ...

CGImageRef imageRef = [image CGImage]; 
    NSUInteger width = rectIntersect.size.width; 
    NSUInteger height = rectIntersect.size.height; 

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char)); 
    NSUInteger bytesPerPixel = 4; 
    NSUInteger bytesPerRow = bytesPerPixel * width; 
    NSUInteger bitsPerComponent = 8; 
    CGContextRef context = CGBitmapContextCreate(rawData, width, height, 
               bitsPerComponent, bytesPerRow, colorSpace, 
               kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); 
    CGColorSpaceRelease(colorSpace); 

    // rectIntersect down here contains the intersection of the two images... 
    CGContextDrawImage(context, rectIntersect, imageRef); 

답변

2

작동하지 않습니다 일부 코드,하지만 하나 개의 이미지를 필요에 가까워 야 그들과 함께. 하위 이미지를 만들려면 CGImageCreateWithImageInRect()을 사용하십시오. 프레임 워크에서 이미지 데이터를 복사 할 필요는 없습니다. 원래의 이미지 데이터를 참조 할 수 있습니다. 따라서 매우 효율적일 수 있습니다.

이미지를 컨텍스트로 그려야하는 경우 물론 하위 이미지를 그릴 수 있습니다.

+0

내가 CGBitmapContext로 그리는 이유는 특정 색상이나 알파 픽셀의 좌표를 저장해야하기 때문입니다. 그래도 실제로 그려진 결과를 화면에 표시 할 필요는 없습니다. CGBitmapContext없이 원래 픽셀 데이터에 액세스 할 수 있습니까? –

+2

[기술 Q & A QA1509 : CGImage 객체에서 픽셀 데이터 가져 오기] (https://developer.apple.com/library/mac/#qa/qa1509/_index.html) –

+0

CGImageCreateWithImageInRect는 내가 찾고있는 것입니다. 추가 링크를 제공하면 코드를 간소화 할 수 있습니다. 감사! –

관련 문제