通过像素操作更改图像(特定于iphone)

时间:2011-08-22 09:36:17

标签: iphone objective-c

类似的问题必须多次提出,但这个问题有点不同。

我正在使用从另一个图像(rocks.jpg)读取的像素值从照片库中处理ImageView控件中加载的图像的像素。

以下是相同的代码: -

    CGContextRef ctx; 
    CGImageRef imageRef = [self.workingImage CGImage];
    UIImage* image2 = [UIImage imageNamed:@"rocks.jpg"]; 

    NSUInteger width = CGImageGetWidth(imageRef);
    NSUInteger height = CGImageGetHeight(imageRef);

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceRGB();
    CGColorSpaceRef colorSpace3 = CGColorSpaceCreateDeviceRGB();

    CGImageRef cgimage2 = image2.CGImage;

    NSUInteger width2  = CGImageGetWidth(cgimage2);
    NSUInteger height2 = CGImageGetHeight(cgimage2);

    struct pixel* pixels = (struct pixel*) calloc(1, width * height * sizeof(struct pixel));

    struct pixel* pixels2 = (struct pixel*) calloc(1, width2 * height2 * sizeof(struct pixel)); 

    NSUInteger bytesPerPixel = 4;
    NSUInteger bytesPerRow = bytesPerPixel * width;
    NSUInteger bitsPerComponent = 8;

    NSUInteger bpr2 = bytesPerPixel * width2;
    //size_t bpp2 = CGImageGetBitsPerPixel(cgimage2);
    NSUInteger bpc2 = 8;

    CGContextRef context = CGBitmapContextCreate(pixels, width, height,
                                                 bitsPerComponent, bytesPerRow, colorSpace,
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGColorSpaceRelease(colorSpace);

    CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);


    CGContextRef context2 = CGBitmapContextCreate(pixels2, width2, height2, bpc2, bpr2, colorSpace2,kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

    CGColorSpaceRelease(colorSpace2);

    CGContextDrawImage(context2, CGRectMake(0, 0, width2, height2),cgimage2);


    NSUInteger numberOfPixels = width*height;

    while (numberOfPixels > 0) 
    {
        if(pixels->g == 250)
        {
            pixels->r = pixels2->r;
            pixels->g = pixels2->g;
            pixels->b = pixels2->b;
            pixels->a = pixels2->a;
//            pixels->r = 0;
//            pixels->g = 0;
//            pixels->b = 0;
        }
        pixels++;
        pixels2++;
        numberOfPixels--;
    }


    ctx = CGBitmapContextCreate(pixels,  
                                width,  
                                height,  
                                bitsPerComponent,  
                                bytesPerRow,  
                                colorSpace3,  
                                kCGImageAlphaPremultipliedLast|kCGBitmapByteOrder32Big ); 

    if (ctx == NULL)
    {

        NSLog(@"The context is null");

    }

    CGColorSpaceRelease(colorSpace3);

    CGImageRef img2 = nil;

        if (ctx != nil) 
        {
            img2 = CGBitmapContextCreateImage (ctx);
        }


    //CGContextDrawImage(ctx, CGRectMake(0, 0, width, height), rawImage.CGImage );

    UIImage* rawImage = [UIImage imageWithCGImage:img2];

    CGContextRelease(context);
    CGContextRelease(context2);
    CGContextRelease(ctx);

    //self.workingImage = rawImage;  
    //[self.imageView setImage:self.workingImage];

    //[imageView setImage:rawImage];

    imageView.image = rawImage;


    if (pixels != NULL)
    {
        pixels = NULL;
        free(pixels);
    }

    if (pixels2 != NULL)
    {
        pixels2 = NULL;
        free(pixels2);
    }

问题是虽然我可以读取像素值并操纵它们但最终结果是第二个图像(rocks.jpg)被加载到视图中而不是被加载的被操纵图像。

有人可以帮我解决这个问题。??

提前致谢。

0 个答案:

没有答案