UIImage圆angular

我已经阅读了几个post。 几乎所有人都build议在layer.cornerRadius使用QuartzCore/QuartzCore.h layer.cornerRadius

我已经尝试过这种方法和一些更多的方法。

那么,一个图像不移动时,一切正常。

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

这是我调整图像的方式:

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

我想知道一个调整图像大小的好办法,增加阴影,圆angular半径,还可以使图像顺利移动。

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

尝试移动这个代码,据我所知,我用了一段时间,这使得一个圆angular的图像,你可以移动到目标。 但它似乎很好的规模…

罗汉的回答是一个很好的答案。 如果任何人有任何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; } 

Charlie Seligman的解决scheme有更优化的内存版本。 你不需要使用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)来获得圆形图像。

导入.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; 

希望,这可以帮助你….

如果在dynamic显示图像位置时性能不好,可能是因为每帧都必须渲染图像的拐angular半径。 这是不可避免的,如果图像背后没有一个纯色(“混合”是不同的每一帧,因此必须计算)。 如果情况并非如此,并且可以使背景变为纯色,请确保将UIView.backgroundColor设置为UIView.backgroundColor以外的其他值,并使用alpha 1.0。 同样栅格化图层可能会帮助你(它会将渲染图层的caching版本存储在内存中,并在整个animation中使用该图层,但是如果图层不透明则不起作用)。

这是你如何做到这一点:

 UIView *yourView; CALayer *yourLayer = yourView.layer; yourLayer.shouldRasterize = YES; yourLayer.rasterizationScale = [UIScreen mainScreen].scale; 

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

 yourImageView.layer.cornerRadius = yourRadius; yourImageView.clipsToBounds = YES; 

对的,这是可能的。

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

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