iPhone开发应用中Mask用法是本文要介绍的内容,主要是来介绍Mask的用法。内容不多,本文主要是由代码实现,来看详细内容。
mask方法
- + (UIImage*) maskImage:(UIImage*)image withMask:(UIImage*)mask {
- CGImageRef imgRef = [image CGImage];CGImageRef maskRef = [mask CGImage];
- CGImageRef actualMask = CGImageMaskCreate(CGImageGetWidth(maskRef),CGImageGetHeight(maskRef),
- CGImageGetBitsPerComponent(maskRef),CGImageGetBitsPerPixel(maskRef),
- CGImageGetBytesPerRow(maskRef),CGImageGetDataProvider(maskRef),
- NULL, false);CGImageRef masked = CGImageCreateWithMask(imgRef,
- actualMask);return [UIImage imageWithCGImage:masked];
- }
调整图片大小
- + (UIImage *) resizeImage:(UIImage *)image size:(CGSize)newSize {UIGraphicsBeginImageContext(newSize);
- [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
- UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();
- return newImage;
- }
方法调用/:將原始圖片縮成120x120,這一步視原始圖片大小而定,不一定要做
- UIImage *newImg = [Utils resizeImage:orgImg size:CGSizeMake(120, 120)];
- UIImage *maskImg = [UIImage imageNamed:@"mask.png"];
- // 取得mask的圖片物件
- newImg = [ImageUtils maskImage:newImg withMask:maskImg];
- // 開始做裁切(Clip)圖片
小结:详解iPhone开发应用中Mask用法的内容介绍完了,希望通过本文的学习能对你有所帮助!