21

我已经阅读了几篇文章。几乎每个人都建议使用QuartzCore/QuartzCore.hwithlayer.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);

我想知道一种调整图像大小、添加阴影、圆角半径并且仍然可以平滑移动图像的好方法。

4

7 回答 7

71
// 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();

尝试使用此代码移动,据我所知,我不久前使用过它,它制作了一个带有圆角的图像,您可以将其移动到目标中。但它似乎扩展得很好......

于 2012-05-12T13:09:11.093 回答
13

罗汉的回答很好。如果有人有任何重构它以在他们的项目中工作的问题,这里是一个简洁的重构,希望能帮助一些新的程序员:

+ (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;
}
于 2013-04-07T11:01:28.377 回答
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)来获得圆形图像。

于 2014-04-10T18:22:34.933 回答
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;

希望,这对你有帮助....

于 2012-05-12T13:53:55.700 回答
1

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

这是你如何做到这一点:

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

yourLayer.shouldRasterize = YES;
yourLayer.rasterizationScale = [UIScreen mainScreen].scale;
于 2012-05-13T06:34:42.427 回答
1

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

yourImageView.layer.cornerRadius = yourRadius;                                   

yourImageView.clipsToBounds = YES;                                 
于 2016-02-09T06:34:26.107 回答
0

是的,有可能。

导入 QuartzCore (#import) 标头并使用 UIImageView 的 layer 属性。

ImageView.layer.cornerRadius = Radius;
ImageView.clipsToBounds = YES;
于 2017-05-30T06:25:04.493 回答