2

我一直在努力使用这种方法一段时间。我正在另一个图像上绘制一个头像。我想成为一个圆圈的用户图片,但我似乎无法弄清楚如何。用户图片是 aUIImage而不是UIImageView. 如果它是一个图像视图,我知道如何制作一个圆圈。下面是代码。可能有更好的方法。

-(UIImage *)drawImage:(UIImage*)pinImage withBadge:(UIImage *)user{



    UIGraphicsBeginImageContextWithOptions(pinImage.size, NO, 0.0f);
    [pinImage drawInRect:CGRectMake(0, 0, pinImage.size.width, pinImage.size.height)];
    [user drawInRect:CGRectMake(20.0, 10.0, user.size.width/2, user.size.height/2)];
    UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return resultImage;

}

结果很好,但用户图像仍然是正方形,而不是圆形。我尝试将用户图像添加到 a UIImageView,将其转换为圆形,然后通过调用在方法中使用它yourImageView.image,但没有运气。我还尝试了许多其他方法。我的逻辑很可能是不正确的。

所需的结果是在图钉/注释顶部的圆形图像位置。黑点将是图像(比这更大的圆圈)。

在此处输入图像描述

4

2 回答 2

3

您可以将图像上下文剪辑到图像的路径

// Start the image context
UIGraphicsBeginImageContextWithOptions(pinImage.size, NO, 0.0);
UIImage *resultImage = nil;

// Get the graphics context
CGContextRef context = UIGraphicsGetCurrentContext();

// Draw the first image
[pinImage drawInRect:CGRectMake(0, 0, pinImage.size.width, pinImage.size.height)];

// Get the frame of the second image
CGRect rect = CGRectMake(20.0, 10.0, user.size.width/2, user.size.height/2)

// Add the path of an ellipse to the context
// If the rect is a square the shape will be a circle
CGContextAddEllipseInRect(context, rect);
// Clip the context to that path
CGContextClip(context);

// Do the second image which will be clipped to that circle
[user drawInRect:rect];

// Get the result
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext(); 

// End the image context
UIGraphicsEndImageContext();
于 2014-03-13T18:30:17.647 回答
0

创建一个圆形路径,然后剪辑到那个?

CGContextAddArc(ctx, ....);
CGContextClip(ctx);
于 2014-03-13T18:31:26.677 回答