gpt4 book ai didi

ios - 如何创建带边框的圆形图像(UIGraphics)?

转载 作者:可可西里 更新时间:2023-11-01 05:01:04 25 4
gpt4 key购买 nike

如何创建带边框的圆形图像(UIGraphics)?

附言我需要画一幅画。

viewDidLoad 中的代码:

NSURL *url2 = [NSURL URLWithString:@"http://images.ak.instagram.com/profiles/profile_55758514_75sq_1399309159.jpg"];
NSData *data2 = [NSData dataWithContentsOfURL:url2];
UIImage *profileImg = [UIImage imageWithData:data2];

UIGraphicsEndImageContext();
// Create image context with the size of the background image.
UIGraphicsBeginImageContext(profileImg.size);
[profileImg drawInRect:CGRectMake(0, 0, profileImg.size.width, profileImg.size.height)];

// Get the newly created image.
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();

// Release the context.
UIGraphicsEndImageContext();

// Set the newly created image to the imageView.
self.imageView.image = result;

最佳答案

听起来您想将图像剪裁成一个圆圈。这是一个例子:

static UIImage *circularImageWithImage(UIImage *inputImage,
UIColor *borderColor, CGFloat borderWidth)
{

CGRect rect = (CGRect){ .origin=CGPointZero, .size=inputImage.size };

UIGraphicsBeginImageContextWithOptions(rect.size, NO, inputImage.scale); {

// Fill the entire circle with the border color.
[borderColor setFill];
[[UIBezierPath bezierPathWithOvalInRect:rect] fill];

// Clip to the interior of the circle (inside the border).
CGRect interiorBox = CGRectInset(rect, borderWidth, borderWidth);
UIBezierPath *interior = [UIBezierPath bezierPathWithOvalInRect:interiorBox];
[interior addClip];

[inputImage drawInRect:rect];

}

UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return outputImage;
}

结果:

example before/after

关于ios - 如何创建带边框的圆形图像(UIGraphics)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25448825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com