Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImage rounded corners

I try to get rounded corners on a UIImage, what I read so far, the easiest way is to use a mask images. For this I used code from TheElements iPhone Example and some image resize code I found. My problem is that resizedImage is always nil and I don't find the error...

- (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize {     CGSize imageSize = [self size];     float width = imageSize.width;     float height = imageSize.height;      // scaleFactor will be the fraction that we'll     // use to adjust the size. For example, if we shrink     // an image by half, scaleFactor will be 0.5. the     // scaledWidth and scaledHeight will be the original,     // multiplied by the scaleFactor.     //     // IMPORTANT: the "targetHeight" is the size of the space     // we're drawing into. The "scaledHeight" is the height that     // the image actually is drawn at, once we take into     // account the ideal of maintaining proportions      float scaleFactor = 0.0;      float scaledWidth = targetSize.width;     float scaledHeight = targetSize.height;      CGPoint thumbnailPoint = CGPointMake(0,0);      // since not all images are square, we want to scale     // proportionately. To do this, we find the longest     // edge and use that as a guide.      if ( CGSizeEqualToSize(imageSize, targetSize) == NO )     {          // use the longeset edge as a guide. if the         // image is wider than tall, we'll figure out         // the scale factor by dividing it by the         // intended width. Otherwise, we'll use the         // height.          float widthFactor = targetSize.width / width;         float heightFactor = targetSize.height / height;          if ( widthFactor < heightFactor )             scaleFactor = widthFactor;         else             scaleFactor = heightFactor;          // ex: 500 * 0.5 = 250 (newWidth)          scaledWidth = width * scaleFactor;         scaledHeight = height * scaleFactor;          // center the thumbnail in the frame. if         // wider than tall, we need to adjust the         // vertical drawing point (y axis)          if ( widthFactor < heightFactor )             thumbnailPoint.y = (targetSize.height - scaledHeight) * 0.5;          else if ( widthFactor > heightFactor )             thumbnailPoint.x = (targetSize.width - scaledWidth) * 0.5;     }       CGContextRef mainViewContentContext;     CGColorSpaceRef colorSpace;      colorSpace = CGColorSpaceCreateDeviceRGB();      // create a bitmap graphics context the size of the image     mainViewContentContext = CGBitmapContextCreate (NULL, targetSize.width, targetSize.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);      // free the rgb colorspace     CGColorSpaceRelease(colorSpace);          if (mainViewContentContext==NULL)         return NULL;      //CGContextSetFillColorWithColor(mainViewContentContext, [[UIColor whiteColor] CGColor]);     //CGContextFillRect(mainViewContentContext, CGRectMake(0, 0, targetSize.width, targetSize.height));      CGContextDrawImage(mainViewContentContext, CGRectMake(thumbnailPoint.x, thumbnailPoint.y, scaledWidth, scaledHeight), self.CGImage);      // Create CGImageRef of the main view bitmap content, and then     // release that bitmap context     CGImageRef mainViewContentBitmapContext = CGBitmapContextCreateImage(mainViewContentContext);     CGContextRelease(mainViewContentContext);      CGImageRef maskImage = [[UIImage imageNamed:@"Mask.png"] CGImage];      CGImageRef resizedImage = CGImageCreateWithMask(mainViewContentBitmapContext, maskImage);     CGImageRelease(mainViewContentBitmapContext);      // convert the finished resized image to a UIImage      UIImage *theImage = [UIImage imageWithCGImage:resizedImage];      // image is retained by the property setting above, so we can      // release the original     CGImageRelease(resizedImage);      // return the image     return theImage; } 
like image 423
catlan Avatar asked Nov 04 '08 15:11

catlan


People also ask

How do you change the corner radius of a storyboard?

Select the view that you want to round and open its Identity Inspector. In the User Defined Runtime Attributes section, add the following two entries: Key Path: layer. cornerRadius , Type: Number, Value: (whatever radius you want)

How do you make a Uiimage rounded?

Make UIImageView Corners RoundedSetting the corner radius to 100 will make the image view completely rounded. Try different corner radius like 10, 20, 30, 40 to get image corners of different radius. To make the image border and visible I will set the borderWidth and the borderColor.


2 Answers

If you are using a UIImageView to display the image you can simply do the following:

imageView.layer.cornerRadius = 5.0; imageView.layer.masksToBounds = YES; 

And to add a border:

imageView.layer.borderColor = [UIColor lightGrayColor].CGColor; imageView.layer.borderWidth = 1.0; 

I believe that you'll have to import <QuartzCore/QuartzCore.h> and link against it for the above code to work.

like image 133
jessecurry Avatar answered Sep 20 '22 18:09

jessecurry


How about these lines...

// 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, 1.0);  // 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(); 
like image 38
epatel Avatar answered Sep 17 '22 18:09

epatel