2012-08-26 3 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];