I'm looking for a way to add a solid color border to an existent image with Core Image. I've found the filter list reference but there is no one to make it.
Help !!
We need to have the CIImage extent or the CGRect in which we want to create the solid border. Than, We can draw a CIImage forming a solid line in the specified area, and repeat the steps for 3 more times for different positions to draw a complete solid rectangle. Following is the piece of code which will draw a straight solid line above the specified area.
CIImage *overlay1 = [CIImage imageWithColor:[CIColor colorWithRed:255/255.f green:0/255.f blue:0/255.f alpha:1.00f]];
overlay1 = [overlay1 imageByCroppingToRect:image.extent];
overlay1 = [overlay1 imageByApplyingFilter:#"CIPerspectiveTransformWithExtent" withInputParameters:#{#"inputExtent":[CIVector vectorWithCGRect:image.extent],#"inputTopLeft":[CIVector vectorWithCGPoint:CGPointMake(topLeft.x - 5, topLeft.y + 5)],#"inputTopRight":[CIVector vectorWithCGPoint:CGPointMake(topRight.x + 5, topRight.y + 5)],#"inputBottomLeft":[CIVector vectorWithCGPoint:CGPointMake(topLeft.x - 5, topLeft.y )],#"inputBottomRight":[CIVector vectorWithCGPoint:CGPointMake(topRight.x + 5, topRight.y ) ]}];
overlay = [ overlay1 imageByCompositingOverImage:overlay];
I have kept the width for 5 pixels. topLeft , topRight.... are the respective CGPoint for the position. For a complete rectangle you will also need bottomLeft and bottomRight.
Overlay is the original CIImage .
This isn't exactly what you asked about, but it might be better if you just want to display the image with a border (rather than actually drawing a border onto it)...
You can use CALayer to add borders (and rounded corners, shadows, etc.) to any UIView...
// imgView is an instance of UIImageView, but this works with any UIView
imgView.layer.borderWidth = 2.0f;
imgView.layer.borderColor = [[UIColor blackColor] CGColor];
You also need to #import <QuartzCore/QuartzCore.h> and link to the QuartzCore framework for this to work.
Related
I have a shadow image that I would like to draw around the outer edge of a grouped UITableView section. This is the image:
I can get the UIBezierPath that represents the rect I want to draw around, but I can't figure out how to repeat the image along the route of the rect. So far it just fills the rect with the image:
UIImage *patternImg = [UIImage imageNamed:#"cellShadow"];
UIColor *fill = [UIColor colorWithPatternImage:patternImg];
[fill setFill];
CGRect aSectRect = [self rectForSection:0];
UIBezierPath *aSectPath = [self createRoundedPath:aSectRect];
[aSectPath fill];
Is this possible? What do I need to do?
Unfortunately, there's no way to have UIBezierPath use an image as a "brush" which is essentially what you'd want. But you can have CoreGraphics draw a shadow for you:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetShadow(context, CGSizeZero, myShadowRadius);
// Draw your shape here
Now if you draw just one shape it will get a shadow. But if you draw more shapes, each will get its own shadow which might not be what you want. The solution is called a transparency layer, which is not related to CALayers or something but is just some kind of "grouping" in CoreGraphics:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetShadow(context, CGSizeZero, myShadowRadius);
CGContextBeginTransparencyLayer(context, NULL);
// Draw your shapes here.
CGContextEndTransparencyLayer(context);
Between the CGContextBeginTransparencyLayer and CGContextEndTransparencyLayer calls the shadow is disabled. After the CGContextEndTransparencyLayer call, the shadow is applied to everything that has been drawn between begin and end as if it were just one shape.
I'm trying just to create a simple gradient with a label on it that I can overlay on top of a MKMapView. I looked on SO and saw I could use CAGradientLayer rather than override drawRect in UIView. I thought I would give it a shot. In my viewDidLoad, I do this:
CGRect frame = CGRectMake(self.mapView.frame.origin.x, self.mapView.frame.origin.y, 320.0, 44.0);
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = frame;
gradientLayer.backgroundColor = (__bridge CGColorRef)((__bridge id)([UIColor clearColor].CGColor));
[gradientLayer setCornerRadius:12.0];
[gradientLayer setOpacity:0.5];
gradientLayer.colors = #[(id)[UIColor blackColor].CGColor, (id)[UIColor whiteColor].CGColor];
[self.view.layer addSublayer:gradientLayer];
It's on the right path, except the upper left corner and upper right corner have this white underneath. Is there a reason for this? I thought it might be because I didn't set the backgroundColor of the layer or something since that has happened to me on UIViews before, but it doesn't seem like it.
Thanks!
The corners don't have white underneath. They have map underneath. Here are the four corners, zoomed 600%:
When you set the corner radius of a layer, you're clipping that layer. Anything under the layer will show through where you've clipped the rounded layer away.
In your case, you've clipped your gradient layer's corners off, so the map layer underneath shows through without being blended with the gradient.
You need to clip your map layer's corners also. The easiest way is to set the same corner radius on your map layer.
R u include this header files...
#import <QuartzCore/QuartzCore.h>
#import "QuartzCore/CALayer.h"
then use radius.
gradientLayer.layer.cornerRadius = 7.0;
Add QuartzCore.framework
#import <QuartzCore/QuartzCore.h>
yourView.layer.cornerRadius = 12.0f;
I like the way this (http://shakeitphoto.com/) application puts a border around the image.. i would like to do something similar in my application but not sure how should I go about doing it.
Any ideas on how given a UIImage can I wrap a frame around it?
From that website, it appears you want a border with a shadow. There's 2 reasonable options, 3 if you don't care about the shadow.
If you don't care about the shadow, you can just do something like
#import <QuartzCore/QuartzCore.h> // this should be at the top
// inside your view layout code
myImageView.layer.borderColor = [UIColor whiteColor].CGColor
myImageView.layer.borderWidth = 5;
This will give you a 5-pixel white border inset into the view, layered on top of the view's contents (e.g. the image). What it won't give you is a shadow. If you want the shadow, there's 2 other options.
You could just create an image that includes the border and the shadow, and nothing else. Just make everything else alpha-transparent. Then you can simply layer this image on top of the one you want to display (either with 2 imageviews, or by creating a third image out of the 2). This should work fine, but it won't scale to different image sizes. In the case of the linked app, the image size is always the same so they could be using this.
The other option is to simply draw the border and shadow on top of your image in a new image. Here's a bit of sample code that will do this - it creates a new image the same size as your original, but with a white, shadowed border:
- (UIImage *)borderedImage:(UIImage *)image {
// the following NO means the new image has an alpha channel
// If you know the source image is fully-opaque, you may want to set that to YES
UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
[image drawAtPoint:CGPointZero];
CGContextRef ctx = UIGraphicsGetCurrentContext();
const CGFloat shadowRadius = 5;
CGContextSetShadowWithColor(ctx, 0, shadowRadius, [UIColor blackColor].CGColor);
[[UIColor whiteColor] set];
CGRect rect = (CGRect){CGPointZero, image.size};
const CGFloat frameWidth = 5;
rect = CGRectInset(rect, frameWidth / 2.0f, frameWidth / 2.0f);
UIBezierPath *path = [UIBezierPath bezierPathWithRect:rect];
path.lineWidth = frameWidth;
[path stroke];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
// note: getting the new image this way throws away the orientation data from the original
// You could create a third image by doing something like
// newImage = [UIImage imageWithCGImage:newImage.CGImage scale:newImage.scale orientation:image.orientation]
// but I am unsure as to how orientation actually affects rendering (if at all)
UIGraphicsEndImageContext();
return newImage;
}
(note: this code has not been compiled and could contain bugs)
i want to track finger touch and draw the path on top of the imageView.
following is what i did:
In the touch event:
UIgraphicsBeginImageContext(imageView.frame.size);
[imageView.image drawInRect:CGRectMake(0,0,imageView.size.width,imageView.size.height)];
//drawing.....
imageView.image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
however, the drawing opaque the background Image?
how to drawing the path while still can make the background available?
thanks in advance:)
If you're doing simple vector drawing, you can set a color with a non-1.0 alpha value. These shapes will then be translucent when you draw them on top of your image. For example:
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGFloat values[4] = {1.0, 0.0, 0.0, 0.5};
CGColorRef translucentRed = CGColorCreate(space, values);
CGContextSetStrokeColorWithColor(context, translucentRed);
CGContextSetFillColorWithColor(context, translucentRed)
// Do your drawing here
CGColorRelease(translucentRed);
CGColorSpaceRelease(space);
will create a translucent red color and set the current stroke and fill to use that color.
Note that overlapping drawn elements will have darker areas because of this translucency, which might be an effect that you don't want.
i would like to draw a border / shadow around a uiscrollview, i know that i could get there with an additional view or scrollview but dont like the handling an drawbacks but i heard that there should be a possibility to dirctly draw a border to a scrollview and that is what i would prefer.
I am quiet new to iphone developement,any answer would helpful.
If you use the layer property of your scroll view (or any UIView) you can easily get a solid border...
#import <QuartzCore/QuartzCore.h>
...
myView.layer.borderWidth = 2;
myView.layer.borderColor = [UIColor blackColor].CGColor;
You can also use the layer to apply real-time shadows by setting the layer.shadow* properties, but performance can be slow with this technique, so I generally prefer to use the following more complex, but more performant technique. You can create a PNG image with transparency in the middle and shadows around the edge - it needs to have 9 distinct areas: 4 for each corner, 4 for each edge, and a completely transparent 1x1 pixel area in the middle. For example if your shadow extends 6 pixels into your image, your image would be 13x13 with the 6 pixel wide/high borders and the 1x1 area in the middle. Then you set it as a scalable image using:
newImage = [image stretchableImageWithLeftCapWidth:6 topCapHeight:6];
UPDATE: Since iOS 5.0 stretchableImageWithLeftCapWidth:topCapHeight: is deprecated so only use this if you still want to support iOS 4.x devices. If you want to support only iOS 5.0+ devices use this instead:
newImage = [image resizableImageWithCapInsets:UIEdgeInsetsMake(6, 6, 6, 6)];
Then you put the image on the parent view so it takes up the entire area of the scroll view. If you want the shadows to go OVER your scrollable elements, (so your scroll view looks inset/behind the rest of the page) then place a transparent UIView over the top with the shadow image on it so that it shows through to your scroll view behind it.
Dont forget to:
#import <QuartzCore/QuartzCore.h>
If you want to use
myView.layer.borderWidth = 2;
myView.layer.borderColor = [UIColor blackColor].CGColor;
To get the CGColorRef from an UIColor you can also use this example:
myView.layer.borderWidth = 2;
myView.layer.borderColor = [UIColor blackColor].CGColor;
myView.layout.borderColor generates an incompatible pointer warning when using UIColor, and did not change the border for me.
Using CGColorRef worked for me instead:
CGFloat lComponents[4] = {0,0,0,1};
CGColorSpaceRef lColorSpace = CGColorSpaceCreateDeviceRGB();
myView.layer.borderColor = CGColorCreate(lColorSpace, lComponents);
CGColorSpaceRelease(lColorSpace);