2012-08-26 62 views
0

我正在構建一個IOS應用程序。 在我的應用程序中,我有一個用戶可以手動裁剪的圖像。 用戶裁剪圖像後,我希望將掩碼信息 保存爲二進制格式。IOS - 從UIBeizerPath獲取二進制掩碼(位掩碼)

我需要它作爲一個二進制圖像,不僅作爲NSCoding表格, ,因爲我需要使用其他平臺上的掩碼信息 (不僅僅是IOS)。

如何將UIBezierPath轉換爲二進制掩碼數組。

回答

1

首先,您必須柵格化(描邊/填充)您的UIBeizerPath到您的圖像上下文,然後您可以使用它的柵格數據進行操作。這裏是一個如何光柵化UILabel的例子:

UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, siz.w, siz.h)]; 
label.text = [[NSString alloc] ... ]; 
label.font = [UIFont boldSystemFontOfSize: ... ]; 

UIGraphicsBeginImageContext(label.frame.size); 
[label.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage* layerImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

// Get Image size 
GLuint width = CGImageGetWidth(layerImage.CGImage); 
GLuint height = CGImageGetHeight(layerImage.CGImage); 
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 

// Allocate memory for image 
void *imageData = malloc(height * width * 4); 
CGContextRef imgcontext = CGBitmapContextCreate(
               imageData, width, height, 8, 4 * width, colorSpace, 
               kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Little); 
CGColorSpaceRelease(colorSpace); 
CGContextClearRect(imgcontext, 
        CGRectMake(0, 0, width, height)); 
CGContextTranslateCTM(imgcontext, 0, height - height); 
CGContextDrawImage(imgcontext, 
        CGRectMake(0, 0, width, height), layerImage.CGImage); 

// Create image 

[.. here you can do with imageData whatever you want ..] 

image.InitImage(imageData, width * height * 4, width, height, iResource_Image::ImgFormat_RGBA32); 



// Release context 
CGContextRelease(imgcontext); 
free(imageData); 
[label release];