带圆角的UIImage

时间:2012-05-12 13:02:36

标签: ios xcode uiimage rounded-corners

我已经阅读了几篇帖子。几乎所有人都建议将QuartzCore/QuartzCore.hlayer.cornerRadius

一起使用

我尝试过这种方法和更多方法。

好吧,当图像不移动时,一切正常。

但在我的项目中,我总是移动我的图像。 如果我添加角半径或阴影,图像移动不再平滑。它看起来很可怕!

这就是我调整图片大小的方式:

CGSize newSize = CGSizeMake(200*height/image.size.height, 280);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(nil, newSize.width, newSize.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);
CGContextClearRect(context, CGRectMake(0, 0, newSize.width, newSize.height));
CGContextDrawImage(context, CGRectMake(0, 0, newSize.width, newSize.height), image.CGImage);
CGImageRef scaledImage = CGBitmapContextCreateImage(context);
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
UIImage *newImage = [UIImage imageWithCGImage: scaledImage];
CGImageRelease(scaledImage);

我想知道调整图像大小,添加阴影,角半径以及图像平滑移动的好方法。

7 个答案:

答案 0 :(得分:70)

// Get your image somehow
UIImage *image = [UIImage imageNamed:@"image.jpg"];

// Begin a new image that will be the new image with the rounded corners 
// (here with the size of an UIImageView)
 UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, [UIScreen mainScreen].scale);

 // Add a clip before drawing anything, in the shape of an rounded rect
  [[UIBezierPath bezierPathWithRoundedRect:imageView.bounds 
                        cornerRadius:10.0] addClip];
 // Draw your image
[image drawInRect:imageView.bounds];

 // Get the image, here setting the UIImageView image
  imageView.image = UIGraphicsGetImageFromCurrentImageContext();

 // Lets forget about that we were drawing
  UIGraphicsEndImageContext();

尝试使用此代码移动,据我所知,我曾经使用过一段时间,这使得带有圆角的图像可以移动到目标中。但似乎规模很好......

答案 1 :(得分:12)

罗汉的答案很棒。如果有人有任何probs重构它在他们的项目中工作这里是一个整洁的重构,可能希望帮助一些新的程序员:

+ (UIImage *)imageWithRoundedCornersSize:(float)cornerRadius usingImage:(UIImage *)original
{    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:original];

    // Begin a new image that will be the new image with the rounded corners
    // (here with the size of an UIImageView)
    UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0);

    // Add a clip before drawing anything, in the shape of an rounded rect
    [[UIBezierPath bezierPathWithRoundedRect:imageView.bounds
                            cornerRadius:cornerRadius] addClip];
    // Draw your image
    [original drawInRect:imageView.bounds];

    // Get the image, here setting the UIImageView image
    imageView.image = UIGraphicsGetImageFromCurrentImageContext();

    // Lets forget about that we were drawing
    UIGraphicsEndImageContext();

    return imageView.image;
}

答案 2 :(得分:10)

Charlie Seligman的解决方案有更优化的内存版本。您不需要为此目的使用UIImageView。

+ (UIImage *)imageWithRoundedCornersSize:(float)cornerRadius usingImage:(UIImage *)original
{
    CGRect frame = CGRectMake(0, 0, original.size.width, original.size.height);

    // Begin a new image that will be the new image with the rounded corners
    // (here with the size of an UIImageView)
    UIGraphicsBeginImageContextWithOptions(original.size, NO, original.scale);

    // Add a clip before drawing anything, in the shape of an rounded rect
    [[UIBezierPath bezierPathWithRoundedRect:frame
                                cornerRadius:cornerRadius] addClip];
    // Draw your image
    [original drawInRect:frame];

    // Get the image, here setting the UIImageView image
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    // Lets forget about that we were drawing
    UIGraphicsEndImageContext();

    return image;
}

您可以通过指定cornerRadius等于两次增加的图像大小(image.size.width * 2)来获得圆形图像。

答案 3 :(得分:2)

在导入.m文件中的#import文件后尝试下面的代码

  yourImageView.layer.shadowColor=[[UIColor grayColor] CGColor];
  yourImageView.layer.cornerRadius = 8;
  yourImageView.layer.shadowOffset=CGSizeMake(2, 2);
  yourImageView.layer.shadowOpacity=1.0;
  yourImageView.layer.shadowRadius=1.0;

希望,这有助于你......

答案 4 :(得分:1)

如果在动画图像位置时性能不好,可能是因为它必须每帧渲染一个角半径的图像。如果在图像后面没有纯色(在每帧中“混合”不同,因此必须计算),这是不可避免的。如果不是这种情况,并且您可以将背景设置为纯色,请确保将UIView.backgroundColor设置为clearColor以外的其他内容,并使用alpha 1.0。 栅格化图层可能对您有所帮助(它会将渲染图层的缓存版本存储在内存中,并在整个动画中使用它。但是,如果图层不是不透明的话,也无济于事。)

这是你这样做的:

UIView *yourView;
CALayer *yourLayer = yourView.layer;

yourLayer.shouldRasterize = YES;
yourLayer.rasterizationScale = [UIScreen mainScreen].scale;

答案 5 :(得分:1)

在Objective-C中设置图像上的圆角:

yourImageView.layer.cornerRadius = yourRadius;                                   

yourImageView.clipsToBounds = YES;                                 

答案 6 :(得分:1)

是的,这是可能的。

导入QuartzCore(#import)标题并使用UIImageView的layer属性进行播放。

ImageView.layer.cornerRadius = Radius;
ImageView.clipsToBounds = YES;