//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)圖片
from:http://hi.baidu.com/%D3%D0%CF%E3%D1%CC%C3%BB%BB%F0%B2%F1/blog/item/33f2038d6880a3a70e24444e.html