14

Tôi đang cố tạo mặt nạ hình ảnh từ tổng hợp của hai hình ảnh hiện có.Tạo mặt nạ với CGImageMaskCreate là tất cả màu đen (iphone)

Trước tiên tôi bắt đầu với việc tạo ra các hỗn hợp trong đó bao gồm một hình ảnh nhỏ mà là hình ảnh mặt nạ, và một hình ảnh lớn hơn đó là kích thước tương tự như nền:

UIImage * BaseTextureImage = [UIImage imageNamed:@"background.png"]; 
UIImage * MaskImage = [UIImage imageNamed:@"my_mask.jpg"]; 
UIImage * ShapesBase = [UIImage imageNamed:@"largerimage.jpg"]; 
UIImage * MaskImageFull; 

CGSize finalSize = CGSizeMake(480.0, 320.0); 
UIGraphicsBeginImageContext(finalSize); 
[ShapesBase drawInRect:CGRectMake(0, 0, 480, 320)]; 
[MaskImage drawInRect:CGRectMake(150, 50, 250, 250)]; 
MaskImageFull = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

tôi có thể sản lượng UIImage này (MaskImageFull) và nó có vẻ đúng, nó là một kích thước nền fullsize và nó là một nền trắng với đối tượng mặt nạ của tôi trong màu đen, ở đúng nơi trên màn hình.

sau đó tôi vượt qua MaskImageFull UIImage qua chuyện này:

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); 
UIImage* retImage= [UIImage imageWithCGImage:masked]; 

Vấn đề là các retImage là tất cả màu đen. Nếu tôi gửi một UIImage được tạo sẵn trong khi mặt nạ nó hoạt động tốt, nó chỉ là khi tôi cố gắng làm cho nó từ nhiều hình ảnh mà nó phá vỡ.

Tôi nghĩ đó là một điều không gian màu nhưng dường như không thể khắc phục được. Bất kỳ sự giúp đỡ nào cũng được đánh giá cao!

Trả lời

19

Tôi đã thử điều tương tự với CGImageCreateWithMask và nhận được kết quả tương tự. Giải pháp mà tôi đã tìm thấy là sử dụng CGContextClipToMask thay thế:

CGContextRef mainViewContentContext; 
CGColorSpaceRef colorSpace; 

colorSpace = CGColorSpaceCreateDeviceRGB(); 

// create a bitmap graphics context the size of the image 
mainViewContentContext = CGBitmapContextCreate (NULL, targetSize.width, targetSize.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast); 

// free the rgb colorspace 
CGColorSpaceRelease(colorSpace);  

if (mainViewContentContext==NULL) 
    return NULL; 

CGImageRef maskImage = [[UIImage imageNamed:@"mask.png"] CGImage]; 
CGContextClipToMask(mainViewContentContext, CGRectMake(0, 0, targetSize.width, targetSize.height), maskImage); 
CGContextDrawImage(mainViewContentContext, CGRectMake(thumbnailPoint.x, thumbnailPoint.y, scaledWidth, scaledHeight), self.CGImage); 


// Create CGImageRef of the main view bitmap content, and then 
// release that bitmap context 
CGImageRef mainViewContentBitmapContext = CGBitmapContextCreateImage(mainViewContentContext); 
CGContextRelease(mainViewContentContext); 

// convert the finished resized image to a UIImage 
UIImage *theImage = [UIImage imageWithCGImage:mainViewContentBitmapContext]; 
// image is retained by the property setting above, so we can 
// release the original 
CGImageRelease(mainViewContentBitmapContext); 

// return the image 
return theImage; 
+1

Đã giúp tôi rất nhiều.Bạn có thể thêm đầu trang và chân trang của hàm để làm cho nó hoàn chỉnh không? – Eden

+0

thumbnailPoint.x chính xác là gì ??! @catlan –

+0

@ Reza.Ab xin lỗi tôi không nhớ. thu nhỏ * cũng có vẻ kỳ lạ 9 năm sau đó. Tôi giả định trong hầu hết các trường hợp mặt nạ rect và hình ảnh rect nên giống nhau. Hãy thử nó và cho chúng tôi biết những gì bạn tìm thấy, – catlan

1

Hình ảnh được che mặt PHẢI được tạo bằng kênh alpha. Kênh Alpha có thể không được tạo từ mã.

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

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 

    UIImage *maskImage = [UIImage imageNamed:@"MaskFinal.png"]; 
    CGImageRef maskImageRef = [maskImage CGImage]; 

    // create a bitmap graphics context the size of the image 
    CGContextRef mainViewContentContext = CGBitmapContextCreate (NULL, maskImage.size.width, maskImage.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast); 


    if (mainViewContentContext==NULL) 
     return NULL; 

    CGFloat ratio = 0; 

    ratio = maskImage.size.width/ image.size.width; 

    if(ratio * image.size.height < maskImage.size.height) { 
     ratio = maskImage.size.height/ image.size.height; 
    } 

    CGRect rect1 = {{0, 0}, {maskImage.size.width, maskImage.size.height}}; 
    CGRect rect2 = {{-((image.size.width*ratio)-maskImage.size.width)/2 , -((image.size.height*ratio)-maskImage.size.height)/2}, {image.size.width*ratio, image.size.height*ratio}}; 


    CGContextClipToMask(mainViewContentContext, rect1, maskImageRef); 
    CGContextDrawImage(mainViewContentContext, rect2, image.CGImage); 


    // Create CGImageRef of the main view bitmap content, and then 
    // release that bitmap context 
    CGImageRef newImage = CGBitmapContextCreateImage(mainViewContentContext); 
    CGContextRelease(mainViewContentContext); 

    UIImage *theImage = [UIImage imageWithCGImage:newImage]; 

    CGImageRelease(newImage); 

    // return the image 
    return theImage; 
} 
+0

Không hoạt động thực sự ... –

Các vấn đề liên quan