在iOS中使用棕褐色图像效果时出错?

时间:2014-02-20 09:45:23

标签: ios uiimageview effects

在我的应用中,我有一个imageViewimageView中的图片应该会显示棕褐色,黑色和白色等效果。
为了做棕褐色效果,我使用了以下代码,

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

    int imagWidth = image.size.width;
    int imageheight = image.size.height;
    NSInteger myDataLength = imagWidth * imageheight * 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 * imagWidth;
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
    CGImageRef imageRef = CGImageCreate(imagWidth, imageheight, 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;
}

它工作正常,但仅适用于.png图像,当我使用.jpg图像时,它只显示imageView的黑色视图。任何帮助将不胜感激

3 个答案:

答案 0 :(得分:1)

使用核心图像处理,

 -(void)makeSepiaScale
{
CIImage *beginImage =[[CIImage alloc]initWithImage:imageForView];

    CIContext * context=[CIContext contextWithOptions:nil];

    CIFilter *filter = [CIFilter filterWithName:@"CISepiaTone"
                                  keysAndValues: kCIInputImageKey, beginImage,
                        @"inputIntensity", @0.8, nil];
    CIImage *outImage = [filter outputImage];

    CGImageRef cgimg=[context createCGImage:outImage fromRect:[outImage extent]];


[imageView setImage:[UIImage imageWithCGImage:cgimg]];

}

答案 1 :(得分:0)

使用CGImageCreateWithJPEGDataProvider从JPEG获取位图:

-(UIImage*)makeSepiaScale:(UIImage*)image {

    CGImageRef cgJpegImage = [image CGImage];
    CGDataProviderRef jpegProvider = CGImageGetDataProvider(cgJpegImage);

    CGImageRef cgBitmapImage = CGImageCreateWithJPEGDataProvider(jpegProvider, nil, NO, kCGRenderingIntentRelativeColorimetric);
    CGDataProviderRef bitmapProvider = CGImageGetDataProvider(cgBitmapImage);

    CFDataRef bitmapData = CGDataProviderCopyData(bitmapProvider);
    UInt8* data = (UInt8*)CFDataGetBytePtr(bitmapData);

    // other code to make sepia effect
}

您无法使用纯RGB等JPEG编码图像。所以,首先用CGImageCreateWithJPEGDataProvider解码它。

答案 2 :(得分:0)

我怀疑你正在处理非RGBA格式数据.... JPG for one没有Alpha通道。我没有处理数据的复杂性,而是建议您考虑使用Core Image过滤器,我已经将它们与jpg和PNG一起使用而没有任何问题。

有各种核心图像过滤器,其中一个用于棕褐色,另一个用于灰度/黑白,这里是棕褐色的代码:

-(UIImage*)makeSepiaScale:(UIImage*)input_image output:(UIImage *)output
{

 CIImage *cimage = [[CIImage alloc] initWithImage:input_image];  
CIFilter *myFilter = [CIFilter filterWithName:@"CISepiaTone"];

[myFilter setDefaults];
[myFilter setValue:cimage forKey:@"inputImage"];
[myFilter setValue:[NSNumber numberWithFloat:0.8f] forKey:@"inputIntensity"];

CIImage *image = [myFilter outputImage];
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgImage = [context createCGImage:image fromRect:image.extent];
output = [UIImage imageWithCGImage:cgImage];  
CGImageRelease(cgImage);
return (output);}

PS关于CoreImage的Apple文档很好,我建议你仔细阅读。