ios - 带有圆角的 UIImage

标签 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);

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

最佳答案

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

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

关于ios - 带有圆角的 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10563986/

相关文章:

ios - 如何: Local MP4 Video as Background in iOS App Objective C iOS 8

ios - 在横向模式下将 UIView 放置在 UINavigationcontroller 之上

ios - 自定义字体显示在 Storyboard上,但不显示在模拟器、设备上

ios - iOS 5和iOS 6之间的UIBarButtonItem外观差异

ios - 如何在 iOS 中缩放 UIScrollview 内部 Imageview?

iphone - iOS:使用父类(super class)的 getter 和 setter 的属性不再适用于 Xcode 4.4

objective-c - 从 Info plist 获取数据

ios - 尝试为 iPhone 模拟器构建 Unity 应用程序时出现 Apple Mach O 链接器错误

ios - 试图将图像存储到 sqlite 中,当我尝试检索它时,它是一个字节而不是 200 万字节

ios - 应用程序分配 650MB 的 RAM 以加载 CG 光栅数据 ~250 个 UIImages