我正在尝试使图像的角落变圆,但ImageContext中的图像会失真。
这是我正在采取的步骤:
- (UIImage *)decodeBase64ToImage:(NSString *)strEncodedData
{
NSURL *url = [NSURL URLWithString:strEncodedData];
NSData* data = [[NSData alloc] initWithContentsOfURL:url];
UIImage* image = [[UIImage alloc] initWithData:data];
UIImage* croppedImage = [self makeRoundedImage:image];
return croppedImage;
}
- (UIImage *)makeRoundedImage:(UIImage *) image
{
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
UIGraphicsBeginImageContext(frame.size);
//[[UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, image.size.width, image.size.height) cornerRadius:10.0] addClip];
[image drawInRect:frame];
// Get the image
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
// Lets forget about that we were drawing
UIGraphicsEndImageContext();
return croppedImage;
}
答案 0 :(得分:0)
试试这个。
-(UIImage *)imageWithRoundedCorner:(float)cornerRadius Image:(UIImage *)realImage
{
UIImageView *imageView = [[UIImageView alloc] initWithImage:realImage];
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0);
[[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:cornerRadius] addClip];
[realImage drawInRect:imageView.bounds];
imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return imageView.image;
}
答案 1 :(得分:0)
您可以查看此方法
-(UIImage *)roundedImage:(UIImage *) roundImage
radius: (float) radius;
{
CALayer *imageLayer = [CALayer layer];
imageLayer.frame = CGRectMake(0, 0, roundImage.size.width, roundImage.size.height);
imageLayer.contents = (id) roundImage.CGImage;
imageLayer.masksToBounds = YES;
imageLayer.cornerRadius = radius;
UIGraphicsBeginImageContext(roundImage.size);
[imageLayer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *finalRoundedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return finalRoundedImage;
}
答案 2 :(得分:0)
试试这个。它会解决图像失真的问题。
- (UIImage *)makeRoundedImage:(UIImage *) image
{
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
UIGraphicsBeginImageContextWithOptions(frame.size, NO, 0);
//[[UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, image.size.width, image.size.height) cornerRadius:10.0] addClip];
[image drawInRect:frame];
// Get the image
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
// Lets forget about that we were drawing
UIGraphicsEndImageContext();
return croppedImage;
}