2013-10-15 90 views
0

我在NSbundle中有圖像。以下是圖像如何從圖像創建蒙版

http://tinypic.com/view.php?pic=2093tvt&s=5

我想用這個圖像作爲掩膜圖像和要應用的任何圖像,使其它圖像會在相同的形狀,上面的圖像形狀。

所以它是一個重複的問題,但它不是爲我工作..

如上問題,有人建議grascale圖像所以我也一樣。我改變了圖像的代碼

- (UIImage *) convertToGreyscale:(UIImage *)i { 

int kRed = 1; 
int kGreen = 2; 
int kBlue = 4; 

int colors = kGreen; 
int m_width = i.size.width; 
int m_height = i.size.height; 

uint32_t *rgbImage = (uint32_t *) malloc(m_width * m_height * sizeof(uint32_t)); 
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
CGContextRef context = CGBitmapContextCreate(rgbImage, m_width, m_height, 8, m_width * 4, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast); 
CGContextSetInterpolationQuality(context, kCGInterpolationHigh); 
CGContextSetShouldAntialias(context, NO); 
CGContextDrawImage(context, CGRectMake(0, 0, m_width, m_height), [i CGImage]); 
CGContextRelease(context); 
CGColorSpaceRelease(colorSpace); 

// now convert to grayscale 
uint8_t *m_imageData = (uint8_t *) malloc(m_width * m_height); 
for(int y = 0; y < m_height; y++) { 
    for(int x = 0; x < m_width; x++) { 
     uint32_t rgbPixel=rgbImage[y*m_width+x]; 
     uint32_t sum=0,count=0; 
     if (colors & kRed) {sum += (rgbPixel>>24)&255; count++;} 
     if (colors & kGreen) {sum += (rgbPixel>>16)&255; count++;} 
     if (colors & kBlue) {sum += (rgbPixel>>8)&255; count++;} 
     m_imageData[y*m_width+x]=sum/count; 
    } 
} 
free(rgbImage); 

// convert from a gray scale image back into a UIImage 
uint8_t *result = (uint8_t *) calloc(m_width * m_height *sizeof(uint32_t), 1); 

// process the image back to rgb 
for(int i = 0; i < m_height * m_width; i++) { 
    result[i*4]=0; 
    int val=m_imageData[i]; 
    result[i*4+1]=val; 
    result[i*4+2]=val; 
    result[i*4+3]=val; 
} 

// create a UIImage 
colorSpace = CGColorSpaceCreateDeviceRGB(); 
context = CGBitmapContextCreate(result, m_width, m_height, 8, m_width * sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast); 
CGImageRef image = CGBitmapContextCreateImage(context); 
CGContextRelease(context); 
CGColorSpaceRelease(colorSpace); 
UIImage *resultUIImage = [UIImage imageWithCGImage:image]; 
CGImageRelease(image); 

free(m_imageData); 

// make sure the data will be released by giving it to an autoreleased NSData 
[NSData dataWithBytesNoCopy:result length:m_width * m_height]; 

return resultUIImage; 
} 

灰度下面是灰度圖像

鏈接1:

,當我掩蓋了圖像,然後在結果我得到下面的意見的結果給出 鏈接。我無法添加超過2個鏈接

鏈接2:

我想我還沒有建立適當的掩模圖像

我試圖掩蓋下面的圖片 http://tinypic.com/view.php?pic=10di24m&s=5

任何一個可以告訴的方式製作蒙版圖像?

+0

鏈接1:http://tinypic.com/view.php?pic=2nbzme8&s=5#.Ul0OiGT09e4鏈接2:http://tinypic.com/view.php?pic=21l52c5&s=5 – 287986

+0

的可能重複[通過用手指觸摸來裁剪圖像](http://stackoverflow.com/questions/19362718/crop-an-image-by-drawing-with-finger-touches) – cescofry

回答

0

您使用的方法graysclae似乎存在問題。它使圓圈外的顏色爲黑色,但它應該是白色的,以便正確遮罩。

這裏是我已經寫入

的UIImage + Mask.h

#import <UIKit/UIKit.h> 

@interface UIImage (Mask) 

- (UIImage*) maskImage:(UIImage *) image; 
- (UIImage *)greyscaleImage; 
@end 

的UIImage + Mask.m

@implementation UIImage (Mask) 


- (UIImage*) maskImage:(UIImage *) image 
{ 
    CGImageRef imageReference = image.CGImage; 
    CGImageRef maskReference = self.CGImage; 

    CGImageRef imageMask = CGImageMaskCreate(CGImageGetWidth(maskReference), 
              CGImageGetHeight(maskReference), 
              CGImageGetBitsPerComponent(maskReference), 
              CGImageGetBitsPerPixel(maskReference), 
              CGImageGetBytesPerRow(maskReference), 
              CGImageGetDataProvider(maskReference), 
              NULL, // Decode is null 
              YES // Should interpolate 
              ); 

    CGImageRef maskedReference = CGImageCreateWithMask(imageReference, imageMask); 
    CGImageRelease(imageMask); 

    UIImage *maskedImage = [UIImage imageWithCGImage:maskedReference]; 
    CGImageRelease(maskedReference); 

    return maskedImage; 
} 

- (UIImage *)greyscaleImage { 

    CGImageRef imgRef = self.CGImage; 

    // Get the image width 
    CGFloat width = CGImageGetWidth(imgRef); 
    CGFloat height = CGImageGetHeight(imgRef); 

    // Create a Grayscale colour space 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); 

    /* 
    * Create the context - based on orientation 
    */ 
    CGContextRef context = nil; 

    // If the orientation isn't UIImageOrientationUp then we'll need to rotate 
    UIImageOrientation orient = self.imageOrientation; 

    switch (orient) { 
     case UIImageOrientationUp: 
     case UIImageOrientationDown: 
     case UIImageOrientationDownMirrored: 
     case UIImageOrientationUpMirrored: 

      context = CGBitmapContextCreate(NULL, width, height, 8, 0, colorSpace, kCGImageAlphaNone); 
      break; 

     default: 
      context = CGBitmapContextCreate(NULL, height, width, 8, 0, colorSpace, kCGImageAlphaNone); 
      break; 
    } 

    CGColorSpaceRelease(colorSpace); 

    /* 
    * Create a context translation/rotation based on the orientation 
    */ 
    switch(orient) { 
     case UIImageOrientationUpMirrored: 
      // 2 = 0th row is at the top, and 0th column is on the right - Flip Horizontal 
      CGContextConcatCTM(context, CGAffineTransformMake(-1.0, 0.0, 0.0, 1.0, width, 0.0)); 
      break; 

     case UIImageOrientationDown: 
      // 3 = 0th row is at the bottom, and 0th column is on the right - Rotate 180 degrees 
      CGContextConcatCTM(context, CGAffineTransformMake(-1.0, 0.0, 0.0, -1.0, width, height)); 
      break; 

     case UIImageOrientationDownMirrored: 
      // 4 = 0th row is at the bottom, and 0th column is on the left - Flip Vertical 
      CGContextConcatCTM(context, CGAffineTransformMake(1.0, 0.0, 0, -1.0, 0.0, height)); 
      break; 

     case UIImageOrientationLeftMirrored: 
      // 5 = 0th row is on the left, and 0th column is the top - Rotate -90 degrees and Flip Vertical 
      CGContextConcatCTM(context, CGAffineTransformMake(0.0, -1.0, -1.0, 0.0, height, width)); 
      break; 

     case UIImageOrientationRight: 
      // 6 = 0th row is on the right, and 0th column is the top - Rotate 90 degrees 
      CGContextConcatCTM(context, CGAffineTransformMake(0.0, -1.0, 1.0, 0.0, 0.0, width)); 
      break; 

     case UIImageOrientationRightMirrored: 
      // 7 = 0th row is on the right, and 0th column is the bottom - Rotate 90 degrees and Flip Vertical 
      CGContextConcatCTM(context, CGAffineTransformMake(0.0, 1.0, 1.0, 0.0, 0.0, 0.0)); 
      break; 

     case UIImageOrientationLeft: 
      // 8 = 0th row is on the left, and 0th column is the bottom - Rotate -90 degrees 
      CGContextConcatCTM(context, CGAffineTransformMake(0.0, 1.0, -1.0, 0.0, height, 0.0)); 
      break; 

     case UIImageOrientationUp: 
      // Nothing to do here 
      break; 
    } 

    /* 
    * Create the new image 
    */ 
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor); 
    CGContextFillRect(context, CGRectMake(0, 0, width, height)); 
    //CGContextFillEllipseInRect(context,); 
    // Draw the image to the context 
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), imgRef); 

    // Get a UIImage from the context 
    CGImageRef imageRef = CGBitmapContextCreateImage(context); 
    UIImage *imageCopy = [UIImage imageWithCGImage:imageRef]; 

    // Release 
    CGImageRelease(imageRef); 
    CGContextRelease(context); 

    return imageCopy; 
} 

@end 

這裏是圖像類別用法

UIImage *maskImage = [UIImage imageNamed:@"maskimage"]; // in your case the red dot image 
UIImage *image = [UIImage imageNamed:@"image"]; // image to mask 
maskImage = [maskImage greyscaleImage]; 
image = [maskImage maskImage:image]; 
self.imageView.image = image;