2011-01-06 4 views
0

, 나는이 같은 색상의 화면을 캡처 할 수 있습니다 알고아이폰은 그레이 스케일 스크린 샷을 찍고 있습니까? 그레이 스케일의 화면을 캡처 할 수있는 간단한 방법이 있는지 궁금 해요

UIGraphicsBeginImageContext(self.view.bounds.size); 
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    NSData *data = UIImagePNGRepresentation(image); 

을 내가있는 UIImage를 만들기 위해 코드 줄에 추가 할 필요가 지금 무엇을 그레이 스케일? 읽어 주셔서 감사합니다.

답변

4

이미지를 그레이 스케일로 변환하면됩니다. this 게시물을 읽으십시오. 행운을 빕니다. 여기

는 방법입니다 : D

#pragma mark - 
#pragma mark Grayscale 

- (UIImage *)convertImageToGrayScale:(UIImage *)image 
{ 
    // Create image rectangle with current image width/height 
    CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height); 

    // Grayscale color space 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); 

    // Create bitmap content with current image size and grayscale colorspace 
    CGContextRef context = CGBitmapContextCreate(nil, image.size.width, image.size.height, 8, 0, colorSpace, kCGImageAlphaNone); 

    // Draw image into current context, with specified rectangle 
    // using previously defined context (with grayscale colorspace) 
    CGContextDrawImage(context, imageRect, [image CGImage]); 

    // Create bitmap image info from pixel data in current context 
    CGImageRef imageRef = CGBitmapContextCreateImage(context); 

    // Create a new UIImage object 
    UIImage *newImage = [UIImage imageWithCGImage:imageRef]; 

    // Release colorspace, context and bitmap information 
    CGColorSpaceRelease(colorSpace); 
    CGContextRelease(context); 
    CFRelease(imageRef); 

    // Return the new grayscale image 
    return newImage; 
} 
+0

나는 이제이 기능을 사용하지 않습니다. : D 당신은 하나를 구현할 수 있습니다. : D –

0

레티 나 디스플레이를위한 규모를 처리 할 수있는 능력을 가진 캠의 코드를 기준으로합니다.

- (UIImage *) toGrayscale 
{ 
// Create image rectangle with current image width/height 
CGRect imageRect = CGRectMake(0, 0, self.size.width * self.scale, self.size.height * self.scale); 

int width = imageRect.size.width; 
int height = imageRect.size.height; 

// the pixels will be painted to this array 
uint32_t *pixels = (uint32_t *) malloc(width * height * sizeof(uint32_t)); 

// clear the pixels so any transparency is preserved 
memset(pixels, 0, width * height * sizeof(uint32_t)); 

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 

// create a context with RGBA pixels 
CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * sizeof(uint32_t), colorSpace, 
             kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast); 

// paint the bitmap to our context which will fill in the pixels array 
CGContextDrawImage(context, CGRectMake(0, 0, width, height), [self CGImage]); 

for(int y = 0; y < height; y++) { 
    for(int x = 0; x < width; x++) { 
     uint8_t *rgbaPixel = (uint8_t *) &pixels[y * width + x]; 

     // convert to grayscale using recommended method: http://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale 
     uint32_t gray = 0.3 * rgbaPixel[RED] + 0.59 * rgbaPixel[GREEN] + 0.11 * rgbaPixel[BLUE]; 

     // set the pixels to gray 
     rgbaPixel[RED] = gray; 
     rgbaPixel[GREEN] = gray; 
     rgbaPixel[BLUE] = gray; 
    } 
} 

// create a new CGImageRef from our context with the modified pixels 
CGImageRef image = CGBitmapContextCreateImage(context); 

// we're done with the context, color space, and pixels 
CGContextRelease(context); 
CGColorSpaceRelease(colorSpace); 
free(pixels); 

// make a new UIImage to return 
UIImage *resultUIImage = [UIImage imageWithCGImage:image 
              scale:self.scale 
             orientation:UIImageOrientationUp]; 

// we're done with image now too 
CGImageRelease(image); 

return resultUIImage; 
} 
관련 문제