I have a custom UILabel with round corners and a drop shadow. I'm using the properties on CALayer to achieve this. Next, I'm trying to save this as a UIImage using renderInContext:. The round corners are maintained, but a black background appears and a loose the drop shadow.
Any thoughts on rendering the UILabel as an image but maintaining the shadow and rounded corners?
Here's the code I'm using to render the label:
UIGraphicsBeginImageContextWithOptions(label.bounds.size, YES, 0);
[label.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
You can get rounded corners by changing the second argument to "NO" in UIGraphicsBeginImageContextWithOptions.
Related
How can I programmatically apply a circular crop out to a UIImage or a UIImageView? Is it possible, or must I use a graphical mask?
Till now I am using a graphical mask (a png image) but the final crop out result is ovalized, rather than a perfect circle.
[imageView setContentMode: UIViewContentModeScaleAspectFit];
[imageView setContentMode: UIViewContentModeScaleAspectFill];
also do not solve the problem.
Please note that the UIImageView that the image is applied to is already defined exactly as a square in IB.
If the UIImageView is already a square then you can set the corner radius of its layer to half of its width/height
[imageView.layer setCornerRadius:imageView.frame.size.width / 2.0];
[imageView.layer setMasksToBounds:YES];
I have an image of a my face and I just want to apply some filter or color on lips/eyes etc. I have detected the areas but my issue is how to add color there so that is looks natural and mix with existing image.
Please see the image below to have better idea what I need. See color applied on lips which is pretty much natural,
Use Following code to set any color to given filtered area. For Demo, I have used here rectangle to be get painted, you can use any other bounded area to fill up. Here i have used Yellow color to fill up, you can change that color too.
UIImage *image=...;//Add Code for getting Main Image
CGRect imageRect = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
UIGraphicsBeginImageContext(image.size);
[image drawInRect:imageRect];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 1.0,1.0,0.0,0.5);//set RGBA for color to fill in given area
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 0.0, 1.0);//set RGBA for Border color to add in given area
CGRect rect=CGRectMake(5,5,10,10);//Here, Use the area to be coloured, rightnow i have used Rectangle
CGContextFillRect(context,rect);
CGContextStrokeRectWithWidth(context, rect, 3);//Set Border Width in 3rd argument
CGContextSaveGState(context);
UIImage *newimage = UIGraphicsGetImageFromCurrentImageContext();
[UIImagePNGRepresentation(newimage) writeToFile:FileName] atomically:YES];
UIGraphicsEndImageContext();
There can be multiple ways to do this,one of them can be as mentioned in this link
The other approach can be u can create a UIView of the size same as size of your identified area,with whatever color you want and add that view as a subview on the face .
You can take the image and then apply a CoreImage filter to adjust the colors inside of a masked area that you've defined.
This answer outlines how to do this. Making it look natural more applies to just learning how to create filters that do what you want to the images.
You need to do some drawing customisation for particular image view. Here is some SO Q/A.
how to change the color of an individual pixel in uiimage
iPhone : How to change color of particular pixel of a UIImage?
I have a UIView whose layer has two sublayers, each of which has a 1.5 pixel border around the outside. I am trying to create a UIImage from this view with the following code
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0f);
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:NO];
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
The code does return a UIImage, but the image is clipped – that is, the image doesn't include the all of the borders on the sublayers. I've tried tweaking the sizes/bounds but to no effect. Any suggestions of what else I might try?
Thanks!
What happens if you send the parent layer a
drawInContext: message instead of telling the view to draw itself?
I'm going to use rounded corner box as 'container' of some UIComponents and, of course, it will requires multiple width and height.
my question is... is there any way to have just one PNG file with rounded corner but programatically stretch some area horizontally or vertically so it can be used in multiple UIView. if so, what UIComponent will be used to hold this image? is it UIImageView?
here's what I mean :
You can use below method to stretch imageview :
UIImage *img = [UIImage imageNamed:#"imageName"];
img = [img stretchableImageWithLeftCapWidth:10 topCapHeight:10];
and then its img into imageview.
I know similar question(s) have been asked before, but most answers say create a new detail disclosure button image with the required color (e.g. How to change color of detail disclosure button for table cell).
I want to be able to change it to any color I choose, dynamically, at run-time, so using a pre-configured image is not viable.
From reading around, I think there may be a few ways to do this, but I'm not sure which is the best, or exactly how to do it:
Draw the required color circle in code and overlay with an image of shadow round outside of circle and arrow right (with clear alpha channel for rest, so drawn color circle is still visible)
Add image of shadow round outside of circle in UIImageView, and using as a mask, flood fill within this shadow circle, then overlay the arrow.
Add greyscale image, mask it with itself, and overlay the required color (e.g. http://coffeeshopped.com/2010/09/iphone-how-to-dynamically-color-a-uiimage), then overlay that with arrow image.
What is the best way, and does anyone have any code showing exactly how to do it ?
I use the following helper method to tint a grayscale image. It takes the grayscale image, the tint color, and an option mask image used to ensure the tint only happens in part of the grayscale image. This method also ensure the new image has the same (if any) resizable insets as the original image.
+ (UIImage *)tintImage:(UIImage *)baseImage withColor:(UIColor *)color mask:(UIImage *)maskImage {
UIGraphicsBeginImageContextWithOptions(baseImage.size, NO, baseImage.scale);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect area = CGRectMake(0, 0, baseImage.size.width, baseImage.size.height);
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -area.size.height);
CGContextSaveGState(ctx);
if (maskImage) {
CGContextClipToMask(ctx, area, maskImage.CGImage);
} else {
CGContextClipToMask(ctx, area, baseImage.CGImage);
}
[color set];
CGContextFillRect(ctx, area);
CGContextRestoreGState(ctx);
CGContextSetBlendMode(ctx, kCGBlendModeOverlay);
CGContextDrawImage(ctx, area, baseImage.CGImage);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if (!UIEdgeInsetsEqualToEdgeInsets(baseImage.capInsets, UIEdgeInsetsZero)) {
newImage = [newImage resizableImageWithCapInsets:baseImage.capInsets];
}
return newImage;
}
Here is the non-retina grayscale detail disclosure image:
Here is the retina version:
Here is the non-retina mask (between the quotes - it's mostly white):
""
And the retina mask (between the quotes:
""
Consider also using an icon font in a label instead of an image. Something like glyphicons. Then you can set the text colour to whatever you want. You also have good options for scaling. The cost is that you don't have quite such precise control in some cases but it should work well for your case.