OGeek|极客世界-中国程序员成长平台

标题: ios - ImageContext 返回扭曲的图像 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:46
标题: ios - ImageContext 返回扭曲的图像

我正在尝试使图像的角变圆,但 ImageContext 中的图像变形了。

这是我正在采取的步骤:

- (UIImage *)decodeBase64ToImageNSString *)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 *)makeRoundedImageUIImage *) 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;
}



Best Answer-推荐答案


你可以检查这个方法

-(UIImage *)roundedImageUIImage *) 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;
}

关于ios - ImageContext 返回扭曲的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46423791/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4