如何裁剪图像以使曲线不规则边缘?

时间:2013-04-12 18:08:01

标签: ios objective-c uitableview uiimageview

我有UITableViewCell,图片大小合适。 这就是细胞应该是这样的: enter image description here

我有背景: enter image description here

图片占位符: enter image description here

我想知道是否有办法使用iOS库裁剪图像?

3 个答案:

答案 0 :(得分:1)

是的,可能:

UIImage *imageToCrop = ...;
UIGraphicsBeginImageContext();
CGContextRef context = UIGraphicsGetCurrentContext();
[imageToCrop drawAtPoint:CGPointZero];
CGContextAddEllipseInRect(context, CGRectMake(0 ,0, imageToCrop.size.width,  imageToCrop.size.height);
CGContextClip(context);
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

答案 1 :(得分:0)

您可以使用CoreGraphics添加带路径的蒙版或剪辑。蒙版是带有alpha通道的图像,它确定图像的哪个部分显示。下面的示例如何使用图像蒙版进行剪辑:

- (UIImage *)croppedImage:(UIImage *)sourceImage
{
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), NO, [UIScreen mainScreen].scale);

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClipToMask(context, CGRectMake(0, 0, width, height), [UIImage imageNamed:@"mask"].CGImage);

    [sourceImage drawInRect:CGRectMake(0, 0, width, height)];

    UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resultImage;
}

然后你可以写cell.picture = [self croppedImage:sourceImage];

答案 2 :(得分:0)

您可以使用图像蒙版技术裁剪此图像 请看一下这个链接 https://developer.apple.com/library/mac/documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_images/dq_images.html#//apple_ref/doc/uid/TP30001066-CH212-CJBHIJEB

我写了一些可以帮助你的代码

@interface ImageRenderer : NSObject {
UIImage *image_;
}
@property (nonatomic, retain) UIImage * image;
- (void)cropImageinRect:(CGRect)rect;
- (void)maskImageWithMask:(UIImage *)maskImage;
- (void)imageWithAlpha;
@end

@implementation ImageRenderer

@synthesize image = image_;

- (void)cropImageinRect:(CGRect)rect  {
CGImageRef imageRef = CGImageCreateWithImageInRect(image_.CGImage, rect);
image_ = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
}

- (void)maskImageWithMask:(UIImage *)maskImage  {

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
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;
   }

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);

image_ = [UIImage imageWithCGImage:newImage];

CGImageRelease(newImage);
}

- (void)imageWithAlpha {
CGImageRef imageRef = image_.CGImage;
CGFloat width = CGImageGetWidth(imageRef);
CGFloat height = CGImageGetHeight(imageRef);

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context =  CGBitmapContextCreate(nil, width, height, 8, 0, colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);

CGImageRef resultImageRef = CGBitmapContextCreateImage(context);
image_ = [UIImage imageWithCGImage:resultImageRef scale:image_.scale orientation:image_.imageOrientation];

CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
CGImageRelease(resultImageRef);
}

@end

在此代码中,您可以从较大的图像中裁剪图像,然后您可以使用蒙版图像来完成工作。