代码之家  ›  专栏  ›  技术社区  ›  Chandru

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

  •  1
  • Chandru  · 技术社区  · 10 年前

    在我的应用程序中,我有一个 imageView ,中的图像 图像视图 应该有一些像棕褐色、黑色和白色的效果。
    为了实现棕褐色效果,我使用了以下代码:,

     -(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图像时,它只显示 图像视图 。如有任何帮助,将不胜感激

    3 回复  |  直到 10 年前
        1
  •  1
  •   chandru    10 年前

    使用核心图像处理,

     -(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]];
    

    }

        2
  •  0
  •   Oleg Vit'ko    10 年前

    使用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对其进行解码。

        3
  •  0
  •   Paulo    10 年前

    我怀疑您正在处理非RGBA格式的数据。。。。JPG没有Alpha频道。我建议您考虑使用核心图像过滤器,而不是处理复杂的数据,我已经将它们与jpg和PNG一起使用,没有任何问题。

    有多种核心图像过滤器,其中一种用于Sepia,另一种用于灰度/黑白。这里是Sepia的代码:

    -(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上的苹果文档很好,我建议您仔细阅读。