2011-04-27 4 views
0

NSImage에 포함 된 아이콘에 특별한 색조를주는 간단하고 효율적인 방법을 찾고 있습니다. 세피아 아이콘을 사용하려면 내 초기 ID가 필요하지만 다른 색상을 사용할 수 있으면 더 좋습니다.NSImage의 세피아 버전

이 작업을 수행 할 생각이 있습니까? 당신의 도움에 미리

감사합니다,

감사합니다,

+0

아마도 [이 답변] (http://stackoverflow.com/questions/1117211/how-would-i-tint-an-image-programatically-on-the-iphone/1118005#1118005) (iPhone) 도움이 될 것입니다. –

답변

3

는 당신이 픽셀 씩 걸릴 이미지 픽셀이 있고 효과를 얻기 위해 RGB 값을 조정 것이라 생각합니다.

-(UIImage*)makeSepiaScale:(UIImage*)image 
{ 
    CGImageRef cgImage = [image CGImage]; 
    CGDataProviderRef provider = CGImageGetDataProvider(cgImage); 
    CFDataRef bitmapData = CGDataProviderCopyData(provider); 
    UInt8* data = (UInt8*)CFDataGetBytePtr(bitmapData); 

    int width = image.size.width; 
    int height = image.size.height; 
    NSInteger myDataLength = width * height * 4; 


    for (int i = 0; i < myDataLength; i+=4) 
    { 
     UInt8 r_pixel = data[i]; 
     UInt8 g_pixel = data[i+1]; 
     UInt8 b_pixel = data[i+2]; 

     int outputRed = (r_pixel * .393) + (g_pixel *.769) + (b_pixel * .189); 
     int outputGreen = (r_pixel * .349) + (g_pixel *.686) + (b_pixel * .168); 
     int outputBlue = (r_pixel * .272) + (g_pixel *.534) + (b_pixel * .131); 

     if(outputRed>255)outputRed=255; 
     if(outputGreen>255)outputGreen=255; 
     if(outputBlue>255)outputBlue=255; 


     data[i] = outputRed; 
     data[i+1] = outputGreen; 
     data[i+2] = outputBlue; 
    } 

    CGDataProviderRef provider2 = CGDataProviderCreateWithData(NULL, data, myDataLength, NULL); 
    int bitsPerComponent = 8; 
    int bitsPerPixel = 32; 
    int bytesPerRow = 4 * width; 
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); 
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault; 
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault; 
    CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider2, NULL, NO, renderingIntent); 

    CGColorSpaceRelease(colorSpaceRef); // YOU CAN RELEASE THIS NOW 
    CGDataProviderRelease(provider2); // YOU CAN RELEASE THIS NOW 
    CFRelease(bitmapData); 

    UIImage *sepiaImage = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef); // YOU CAN RELEASE THIS NOW 
    return sepiaImage; 
} 

코드 뻔뻔이 SO 스레드에서 복사하고, 대신 NSImage의있는 UIImage에 세피아 필터를 적용에 대해 이야기하지만 이미지 처리에 관해서 논리는 reused..Also this 스레드가 최고 중 하나입니다 수 있습니다 .. 한 책갈피 ...

편집 : : 조쉬 캐스 웰이 지적했듯이 코어 이미지는 세피아 이미지 및 일부 이미지 필터링을 만드는 데 사용할 수 있습니다. 이렇게 쉬운 방법은 코어 이미지를 사용해야합니다. 어떻게 해야할지에 대한 답변.이 방법은 특히 coreImage 프레임 워크가없는 아이폰에서 잘 작동합니다 ..

+0

직접 할 필요가 없습니다. CoreImage는 작업과 동등합니다. –

+0

정답 조쉬 .. MAC 개발에 익숙하지 않습니다. 내 대답을 편집했습니다. 동정입니다 CoreImage가 아이폰에 없습니다 .. – Krishnabhadra

+0

이제 코어 이미지가 아이폰에 있습니다 .. https://developer.apple.com/library/ ios/# documentation/graphicsimaging/Conceptual/CoreImaging/ci_intro/ci_intro.html – Krishnabhadra

6

CoreImage는 built-in filters이며 그 중 하나는 CISepiaTone이며 이미지의 색상 및 다른 측면을 쉽게 변환 할 수 있습니다.

당신이 Processing an Image에 배치되어 수행해야 할 단계 :

CIContext 객체를 가져옵니다. 가장 간단한 방법은 현재 NSGraphicsContext을 요청하는 것일 수 있습니다.

이미지를 CIImage 대표로 가져옵니다. NSImage에서 직접 생성 할 수는 없습니다. NSImageCGImageForProposedRect:context:hints:을 사용하여 CGImageRef으로 변환해야합니다 (proposedRect 인수에 대해서는 NULL을 전달할 수 있음).

필터 개체를 만들고 값을 설정하고 처리 된 이미지를 가져옵니다.

마지막으로 CIContext에 이미지를 그립니다.