带圆角的UIImage [英] UIImage with rounded corners

查看:94
本文介绍了带圆角的UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了几篇帖子。并且几乎每个人都建议使用 QuartzCore / QuartzCore.h layer.cornerRadius

I have read several posts already. And almost everyone suggest using QuartzCore/QuartzCore.h with layer.cornerRadius

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

I have tried this method and some more methods.

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

Well, everything works fine when an image doesn't move.

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

But in my project I always move my images. If I add corner radius or a shadow the image movement is no longer smooth. And it looks aweful!

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

That is the way I resize my image:

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

我想知道一个很好的方法来调整我的图像大小,添加阴影,角半径仍然有我的形象平滑移动。

I would like to know a good way to resize my image, add shadow, corner radius and still have smooth movement of my image.

推荐答案

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

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

Try moving with this code, as far as I can remember I used this a while back that makes an image with rounded corners that you can move around into the targets. But it seemed to scale fine...

这篇关于带圆角的UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆