簡體   English   中英

如何使用uiimageview修改圖像中的像素並在iPhone中顯示新圖像?

[英]How to modify the pixel in image using uiimageview and display the new image in iPhone?

我是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);   
}

在沒有看到其余代碼的情況下,您可以嘗試以下幾種方法:

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

最后,您不需要CGImageRef。

另外, 在此處查找有關如何以其他方式執行此操作的建議。


[編輯]

我的建議是完全不使用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];
context = CGBitmapContextCreate((void*)pixels,

您不需要創建第二個上下文(這樣做是在泄漏第一個上下文)。 我也不確定為什么您從AlphaPremultipliedLast更改為AlphaPremultipliedFirst。

UIImage *newImage   = [UIImage imageWithCGImage:context];

context是CGContextRef,而不是CGImageRef。 那行不通。

CGImageRef imageRef = CGBitmapContextCreateImage (newImage);   

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM