2015-01-25 3 views
2

두 개의 이미지가 있습니다. 하나는 가장자리/테두리가 다른 투명한 마스크이고 다른 하나는 실제 이미지입니다. 둘 다 병합하고 싶습니다.iOS의 다른 실제 이미지로 투명한 이미지 마스킹

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

    // create a bitmap graphics context the size of the image 
    CGFloat dim = MIN(image.size.width, image.size.height); 
    CGSize size = CGSizeMake(dim, dim); 
    UIGraphicsBeginImageContextWithOptions(size, NO, .0); 
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:(CGRect){ CGPointZero, size }]; 
    [bezierPath fill]; 
    [bezierPath addClip]; 
    CGPoint offset = CGPointMake((dim - image.size.width) * 0.5, (dim - image.size.height) * 0.5); 
    [image drawInRect:(CGRect){ offset, image.size }]; 
    UIImage *ret = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return ret; 
} 

결과 : 결과 이미지에서

enter image description here

, 국경

enter image description hereenter image description here

나는 이미지를 마스크하고 결합하기 위하여 다음 코드를 사용했다 마스크로 사용 된 이미지에이 누락되었습니다. 누군가 이걸로 나를 도울 수 있습니까?

github project

핵심 기능은) 당신의 (예를 들어,이에

를 요약된다 :
+0

당신은 결코 당신의 컨텍스트에 마스크 이미지를 그리거나 당신의 방법에 어떤 식 으로든 maskImage를 사용하지 마십시오 ... –

+0

달성 할 수있는 방법이 있습니다. 나는 어떻게해야할지 모른다. 나는 어딘가에서 코드를 가져왔다. – user3772344

답변

0

나는 (CoreImage에 어쨌든 두 플랫폼 모두에 있기 때문에 잘은 기본적으로 크로스 플랫폼 iOS 용 마스킹 카테고리를 썼다
UIImage *person = ... 
UIImage *circle = ... 
UIImage *result = [person imageMaskedWith:circle]; 

UIImageView *redbox = [[UIImageView alloc] initWithImage:result]; 
redbox.backgroundColor = [UIColor redColor]; //this can be a gradient! 

카테고리의 코드의 주요 부분 :

,745,
CGImageRef imageReference = image.CGImage; 
CGImageRef maskReference = mask.CGImage; 
CGRect rect = CGRectMake(0, 0, CGImageGetWidth(imageReference), CGImageGetHeight(imageReference)); 

// draw with Core Graphics 
UIGraphicsBeginImageContext(rect.size); 
CGContextRef bitmap = UIGraphicsGetCurrentContext(); 
CGContextTranslateCTM(bitmap, 0.0, rect.size.height); 
CGContextScaleCTM(bitmap, 1.0, -1.0); 

CGContextClipToMask(bitmap, rect, maskReference); 
CGContextDrawImage(bitmap, rect, imageReference); 

newImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext();