【问题标题】:Is it possible to trim a UIImage to circular form?是否可以将 UIImage 修剪为圆形?
【发布时间】:2015-05-01 21:51:40
【问题描述】:

我想知道是否可以设置 UIImage 的圆角半径,然后保存修剪后的 UIImage(到 Parse)。

我希望上传到 Parse 的图像是圆形的,我想知道是否可以像这样设置角半径,然后将图像上传到 Parse。会解析保存图像吗?

meImage.layer.cornerRadius = meImage.frame.size.width/2

【问题讨论】:

    标签: ios objective-c parse-platform uiimage


    【解决方案1】:

    您需要绘制图像。试试这个 -

    - (UIImage *)imageByDrawingCircleOnImage:(UIImage *)image
    {
      UIGraphicsBeginImageContext(image.size);
      [image drawAtPoint:CGPointZero];
      CGContextRef ctx = UIGraphicsGetCurrentContext();
      [[UIColor redColor] setStroke];
    
      // make circle rect 5 px from border
      CGRect circleRect = CGRectMake(0, 0,
                              image.size.width,
                                   image.size.height);
      circleRect = CGRectInset(circleRect, 5, 5);
      CGContextStrokeEllipseInRect(ctx, circleRect);
      UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
      return image ;
    

    }

    【讨论】:

    • 这是一个完美的圆吗?
    • 好的。什么是“ctx”。您没有在您提供的代码中显示它
    • 抱歉,复制/粘贴错误。请参考编辑后的答案。
    • 我在UIGraphicsBeginImageContext 之后添加了这个:CGContextRef context = UIGraphicsGetCurrentContext();。另外,我尝试了代码,但没有裁剪它。它只是画一个椭圆。 i.imgur.com/JmrPfJe.jpg
    • 我尝试了 fnc12 的这个解决方案 (stackoverflow.com/questions/7399343/…),但结果却像一个椭圆形:i.imgur.com/lSyTbmd.png
    【解决方案2】:

    嗯,我不知道你为什么接受这个答案,它根本不符合你的要求。

    对您的问题最直接的答案是,不,不可能将图像数据表示修剪为圆形,它们始终是矩形。所以下一个最好的方法是制作图像的部分不在圆圈内透明。
    为了使其正常工作,当您将 NSData 表示上传到服务器时,您需要使用支持透明度的数据表示。在 iOs 中,这意味着使用 PNG 格式而不是 JPG 格式。

    好的,这里有一些代码可以做到这一点。

    //i'd put this in a category on UIImage, therefore we don't need a pointer to the original image, which will be 'self'
    
    -(UIImage *)imageCroppedToCircleOfDiameter:(CGFloat )diameter{
    
    //most images will be rectangular, (height != width)
    //so work out how it is going to scale
    CGFloat scale = CGFloat(fmaxf(diameter/self.size.width, diameter/self.size.height) );
    
    CGRect imageRct = CGRectZero; //where we'l draw the image
    CGRect circleRct = CGRectZero; //where we'll 'cut' a circle from the context
    
    circleRct.size.width = circleRct.size.height = diameter;
    
    imageRct.size.width = self.size.width*scale;
    imageRct.size.height = self.size.height*scale;
    imageRct.origin.x = ((self.size.height>self.size.width)? 0.0: ((imageRct.size.height-imageRct.size.width)/2.0)   );
    imageRct.origin.y = ((self.size.width>self.size.height)? 0.0: ((imageRct.size.width-imageRct.size.height)/2.0)   );
    
    
    
    UIGraphicsBeginImageContextWithOptions(circleRct.size, NO, 0.0);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSaveGState(ctx);
    CGContextAddEllipseInRect( ctx, circleRct);
    CGContextClip(context);
    
    [self drawInRect:imageRct];
    
    
    CGContextRestoreGState(ctx);
    
     UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();
    
     return result;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-08
      • 2011-09-25
      • 1970-01-01
      • 2011-09-19
      • 1970-01-01
      相关资源
      最近更新 更多