2013-08-17 80 views
2

在IOS SDK中,我能夠遮罩圖像但無法反轉圖像遮罩。我的意思是,我有一個使圖像具有矩形部分,現在我掩蓋圖像,我得到附加圖像,但我想要相反的結果。反向圖像遮擋

我得到圖像作爲結果。 enter image description here

雖然我需要這個結果。

enter image description here

請幫我實現它。

...編輯... 我的代碼

UIGraphicsBeginImageContextWithOptions(self.imageView.frame.size, NO, 0.0); 
CGContextRef context = UIGraphicsGetCurrentContext(); 

CGContextTranslateCTM(context, 0.0, self.imageView.frame.size.height); 
CGContextScaleCTM(context, 1.0, -1.0); 

CGImageRef maskImage = [[UIImage imageNamed:@"2.png"] CGImage]; 
CGContextClipToMask(context, self.imageView.bounds, maskImage); 

    CGContextTranslateCTM(context, 0.0, self.imageView.frame.size.height); 
    CGContextScaleCTM(context, 1.0, -1.0); 

[[self.imageView image] drawInRect:self.imageView.bounds]; 

UIImage *image11 = UIGraphicsGetImageFromCurrentImageContext(); 

self.imageView.image = image11; 

感謝

+0

你是如何掩蓋圖像的?顯示你的代碼! –

+0

請看它。我編輯了我的問題並添加了我的代碼.. – Hindu

+1

您是否可以不只是繪製圖像,然後在其上繪製一個白色矩形? – Wain

回答

2

我有兩個步驟來實現它。可能不是最好的辦法,但它的工作原理。

  1. 反轉你的面具圖像。
  2. 面膜

請看下面的代碼。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    _imageView = [[UIImageView alloc] initWithImage:i(@"test1.jpg")]; 
    _imageView.image = [self maskImage:i(@"face.jpg") withMask:[self negativeImage]]; 
    [self.view addSubview:_imageView]; 
} 

下面的方法是從here

- (UIImage *)negativeImage 
{ 
    // get width and height as integers, since we'll be using them as 
    // array subscripts, etc, and this'll save a whole lot of casting 
    CGSize size = self.imageView.frame.size; 
    int width = size.width; 
    int height = size.height; 

    // Create a suitable RGB+alpha bitmap context in BGRA colour space 
    CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB(); 
    unsigned char *memoryPool = (unsigned char *)calloc(width*height*4, 1); 
    CGContextRef context = CGBitmapContextCreate(memoryPool, width, height, 8, width * 4, colourSpace, kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast); 
    CGColorSpaceRelease(colourSpace); 

    // draw the current image to the newly created context 
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), [self.imageView.image CGImage]); 

    // run through every pixel, a scan line at a time... 
    for(int y = 0; y < height; y++) 
    { 
     // get a pointer to the start of this scan line 
     unsigned char *linePointer = &memoryPool[y * width * 4]; 

     // step through the pixels one by one... 
     for(int x = 0; x < width; x++) 
     { 
      // get RGB values. We're dealing with premultiplied alpha 
      // here, so we need to divide by the alpha channel (if it 
      // isn't zero, of course) to get uninflected RGB. We 
      // multiply by 255 to keep precision while still using 
      // integers 
      int r, g, b; 
      if(linePointer[3]) 
      { 
       r = linePointer[0] * 255/linePointer[3]; 
       g = linePointer[1] * 255/linePointer[3]; 
       b = linePointer[2] * 255/linePointer[3]; 
      } 
      else 
       r = g = b = 0; 

      // perform the colour inversion 
      r = 255 - r; 
      g = 255 - g; 
      b = 255 - b; 

      // multiply by alpha again, divide by 255 to undo the 
      // scaling before, store the new values and advance 
      // the pointer we're reading pixel data from 
      linePointer[0] = r * linePointer[3]/255; 
      linePointer[1] = g * linePointer[3]/255; 
      linePointer[2] = b * linePointer[3]/255; 
      linePointer += 4; 
     } 
    } 

    // get a CG image from the context, wrap that into a 
    // UIImage 
    CGImageRef cgImage = CGBitmapContextCreateImage(context); 
    UIImage *returnImage = [UIImage imageWithCGImage:cgImage]; 

    // clean up 
    CGImageRelease(cgImage); 
    CGContextRelease(context); 
    free(memoryPool); 

    // and return 
    return returnImage; 
} 

下面從here採取的方法中採取。

- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage { 

    CGImageRef maskRef = maskImage.CGImage; 

    CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef), 
             CGImageGetHeight(maskRef), 
             CGImageGetBitsPerComponent(maskRef), 
             CGImageGetBitsPerPixel(maskRef), 
             CGImageGetBytesPerRow(maskRef), 
             CGImageGetDataProvider(maskRef), NULL, false); 

    CGImageRef masked = CGImageCreateWithMask([image CGImage], mask); 


    return [UIImage imageWithCGImage:masked]; 

} 
+0

謝謝Vignesh,我已經試過你的代碼,但得到相同的結果與反轉顏色...你有什麼建議... – Hindu

+0

嘗試打印倒置的面具圖像,並檢查它是否反轉你的面具的阿爾法。另一種方法是構建一條路徑(使用Bézier曲線)並執行EO Clip。如果你需要的話,我也可以分享我的面具圖片。 – Vignesh

+0

謝謝,它現在解決.... – Hindu