如何将图像放在自定义路径中

时间:2014-01-31 14:32:38

标签: ios iphone path uiimageview uiimage

我是ios编程的新手,这是我的问题。我使用CGContext(六边形)创建一个路径,我想将图像放入此路径。这是创建路径代码:

-(void)drawAllShapes:(float)xcoord :(float)ycoord :(float)cote  :(CGContextRef)c :(CGFloat *) fillcolor inRect:(CGRect)rect{

xcoord = xcoord*rect.size.width;
ycoord = ycoord*rect.size.height;
cote = cote*rect.size.height;

float x = 0.1835f*rect.size.width;
float y = 0.06162f*rect.size.height;

CGFloat strokecolor[4] = {1.0f, 1.0f, 1.0f, 1.0f};
CGContextSetStrokeColor(c, strokecolor);
CGContextSetFillColor(c, fillcolor);
CGContextSetLineWidth(c, 4.0f);
CGContextBeginPath(c);
CGContextMoveToPoint(c, xcoord, ycoord);
CGContextAddLineToPoint(c, xcoord+x, ycoord+y);
CGContextAddLineToPoint(c, xcoord+x, ycoord+cote+y);
CGContextAddLineToPoint(c, xcoord, ycoord+cote+2*y);
CGContextAddLineToPoint(c, xcoord-x, ycoord+cote+y);
CGContextAddLineToPoint(c, xcoord-x, ycoord+y);
CGContextClosePath(c);
CGContextDrawPath(c, kCGPathFillStroke);    
}

有人能帮助我吗?

3 个答案:

答案 0 :(得分:0)

在您有效CGContext写的地方:

UIImage * image = ...;
float x = 10;
float y = 10;
[image drawInRect:CGRectMake(x, y, image.size.width, image.size.height)];

如果您想缩放图片或拉伸图片,请为image.size.widthimage.size.height使用不同的值

x和y是您想要绘制它的位置。

答案 1 :(得分:0)

您确定要为此目的使用 CGContext 吗?这是另一种方式:

- (UIImage *)maskImage: (UIImage *)image withMask:(UIImage *)maskImage
{
    CGImageRef maskRef = maskImage.CGImage;

    CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                    CGImageGetHeight(maskRef),
                                    CGImageGetBitsPerComponent(maskRef),
                                    CGImageGetBitsPerPixel(maskRef),
                                    CGImageGetBytesPerRow(maskRef),
                                    CGImageGetDataProvider(maskRef), NULL, false);

    CGImageRef maskedImageRef = CGImageCreateWithMask([image CGImage], mask);
    UIImage *maskedImage = [UIImage imageWithCGImage:maskedImageRef];

    CGImageRelease(mask);
    CGImageRelease(maskedImageRef);

    return maskedImage;
}

此功能采用您想要遮罩的图像,以及遮罩,它将应用于初始图像并准确返回您想要的内容即可。您只需要在项目中添加适当的六边形图像。

除此之外,您还可以查看this question

答案 2 :(得分:0)

尝试在此处添加一些内容:

创建属性:

@property(nonatomic) CGPathRef yourPath;
@property(strong, nonatomic) UIImageView *imageView;

在您的绘制方法中,注释掉以下几行:

//CGContextSetFillColor(c, fillcolor);
//CGContextDrawPath(c, kCGPathFillStroke);
//CGContextSetLineWidth(c, 4.0f);

关闭路径后,添加以下内容:

CGContextClosePath(c);

self.yourPath = CGContextCopyPath(c);

CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.bounds = (CGRect){<Specify_Rect>};
shapeLayer.path = self.yourPath;

self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"<Your_Image>"]];
self.imageView.layer.frame = (CGRect){<Specify_Rect>}; //Minor correction: changed to frame.
self.imageView.layer.masksToBounds = YES;
self.imageView.layer.mask = shapeLayer;

[self addSubview:self.imageView];
CGPathRelease(self.path);
CGContextRelease(c);

您应该可以看到您的图像被您的路径“包裹”。我在发布之前测试了它;玩得很开心,享受。