我從以下網址獲取了示例代碼:how to mask an image以屏蔽圖像。屏蔽UIImage時出現問題
代碼是可以正常使用的iPhone模擬器,但不正確地工作在iPhone 4上的模擬器(高清晰度的圖像加載時的...)
這裏是我的代碼和屏蔽功能:
- (void)someMethod {
UIImage* image = [UIImage imageNamed:@"image.png"]; // [email protected] is loaded for high-res device
UIImage* mask = [UIImage imageNamed:@"mask.png"]; // [email protected] is loaded for high-res device
UIImage* maskedImage = [self maskImage:image withMask:mask];
// ... Some code here displaying maskedImage
}
- (UIImage *)maskImage:(UIImage *)image withMask:(UIImage *)maskImage {
UIImage* maskedImage = nil;
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);
maskedImage = [UIImage imageWithCGImage:masked];
return maskedImage;
}
在iPhone 4模擬器上加載[email protected]和[email protected],然後裁剪生成的maskedImage。
任何想法我做錯了什麼?
感謝。您是否有任何示例代碼將對標準和高分辨率圖像執行遮罩工作? – Joshua 2010-09-04 19:37:23