【问题标题】:How to modify the pixel in image using uiimageview and display the new image in iPhone?如何使用 uiimageview 修改图像中的像素并在 iPhone 中显示新图像?
【发布时间】:2010-09-22 12:30:19
【问题描述】:

我是 iphone 新手,我正在制作一个我想更改图像像素的应用程序。我正在使用 uiimageview。我已经完成了一些工作。我认为像素数据发生了变化,但它不显示新的更新图像。 这是我的代码

-(void)Change{
struct pixel {
  unsigned char r, g, b, a;
 };

 UIImage *myimage = [ UIImage imageNamed:@"iphone-wallpapaer-silver-apple.jpg"];

 NSUInteger numberOfRedPixels = 0;
 NSUInteger numberOfGreenPixels = 0;
 NSUInteger numberOfBluePixels = 0;
 NSUInteger numberOfAlphaPixels = 0;
 struct pixel* pixels = (struct pixel*) calloc(1, myimage.size.width * myimage.size.height * sizeof(struct pixel));
    if (pixels != nil)
    {
        // Create a new bitmap

        CGContextRef context = CGBitmapContextCreate(
              (void*) pixels,
              myimage.size.width,
              myimage.size.height,
              8,
              myimage.size.width * 4,
              CGImageGetColorSpace(myimage.CGImage),
              kCGImageAlphaPremultipliedLast
              );

        if (context != NULL)
        {
            // Draw the image in the bitmap

            CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, myimage.size.width, myimage.size.height), myimage.CGImage);

            NSUInteger numberOfPixels = myimage.size.width * myimage.size.height;
   //NSLog(@"%d",numberOfPixels);
            while (numberOfPixels > 0) {
                if (pixels->r == 255) {
                    pixels->r = 0;
     //NSLog(@"%d",pixels->r);
     numberOfRedPixels++;
                }
    if(pixels->g == 255){
     pixels->g = 0;
     //NSLog(@"%d",pixels->g);
     numberOfGreenPixels ++;
    }
    if(pixels->b == 255){
     pixels->b = 0;
     //NSLog(@"%d",pixels->b);
     numberOfBluePixels ++;
    }
    if(pixels->a == 255){
     pixels->a = 0;
     numberOfAlphaPixels ++;
    }
                pixels++;
                numberOfPixels--;
   }

   context = CGBitmapContextCreate((void*)pixels,  
           myimage.size.width,
           myimage.size.height, 
           8,  
           myimage.size.width * 4,
           CGImageGetColorSpace(myimage.CGImage),
           kCGImageAlphaPremultipliedFirst );
UIImage *newImage   = [UIImage imageWithCGImage:context];
CGImageRef imageRef = CGBitmapContextCreateImage (newImage);   
}

【问题讨论】:

    标签: iphone


    【解决方案1】:

    在没有看到其余代码的情况下,您可以尝试几件事:

    [self.myUIImageView setImage:newImage]; // actually sets the image into the view
    [newImage release]; //cleanup memory.
    [self needsDisplay]; //refresh the View
    

    最后你不应该需要 CGImageRef。

    另外,请查看 here 以获取有关如何以替代方式执行此操作的建议。


    [编辑]

    我的建议是完全不要使用 CG。

    NSData *pixelData = UIImagePNGRepresentation(self.UIImageView.UIImage); //psuedo-code
    
    void* pixelBytes = [pixelData bytes];
    
    // Take away the red pixel, assuming 32-bit RGBA
    for(int i = 0; i < [pixelData length]; i += 4) {
        bytes[i] = 0; // red
        bytes[i+1] = bytes[i+1]; // green
        bytes[i+2] = bytes[i+2]; // blue
        bytes[i+3] = bytes[i+3]; // alpha
    }
    NSData* newPixelData = [NSData dataWithBytes:pixelBytes length:[pixelData length]];
    UIImage* newImage = [UIImage imageWithData:newPixelData];
    

    【讨论】:

    • 谢谢,我已经理解了这一点,并且做了一些非常接近它的事情。但问题在于开始。 CGBitmapContextCreate 方法没有正确创建图像
    • 您可以使用上面的bytes[i] 进行替代数学运算。 if(bytes[i]==255) { bytes[i] = 0; redPixels++}; 什么的。
    【解决方案2】:
    context = CGBitmapContextCreate((void*)pixels,
    

    您不需要创建第二个上下文(这样做会泄漏第一个上下文)。我也不确定您为什么将 AlphaPremultipliedLast 更改为 AlphaPremultipliedFirst。

    UIImage *newImage   = [UIImage imageWithCGImage:context];
    

    context 是一个 CGContextRef,而不是一个 CGImageRef。那是行不通的。

    CGImageRef imageRef = CGBitmapContextCreateImage (newImage);   
    

    newImage 是 UIImage*,而不是 CGContextRef。那也行不通。

    【讨论】:

    • 谢谢,我理解你提到的观点,并且已经对 newimage 和 imageref 进行了更改。现在我得到的问题是加载图像。当我使用 CGBitmapContextCreate 创建它并将其复制到我的磁盘以进行确认时。它显示一个空白图像:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-08
    • 2019-08-21
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多