How to make an tiled background like on the web? - iphone

On the iPhone, how could I achieve the same tiled background effect?
For example, I have an pattern image which I want to repeat only horizontally. Would I have to go the route in -drawRect: by myself and loop over to draw the pattern, or is there a convenient method to do that?

You can set the backgroundColor of the UIView with a pattern using the following snippet:
UIImage *image = [UIImage imageNamed:#"pattern.png"];
myView.backgroundColor = [UIColor colorWithPatternImage:image];
The image will automatically be tiled for you to cover the area.
Claus

- (void)drawRect:(CGRect)rect
{
CGContextRef currentContext = UIGraphicsGetCurrentContext();
UIImage* img = [UIImage imageNamed:#"transparency.png"];
CGContextDrawTiledImage(currentContext, CGRectMake(0, 0, 12, 12), [img CGImage]);
}
In this example "transparency.png" is a 12x12 image.

Related

iPhone - Putting and manipulating an Image from the Camera in a UIImageView

This is what I want to do:
Take the camera image an put it in a small (not full screen) UIImageView.
Manipulate the image in realtime (e.g. give half of it a red tint).
I have checked around this subject and uncertain as to the best approach. People are recommending both UIImagePickerController and AVCamCaptureManager, or is there something else.
Could I have your advice?
(void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColor(context, CGColorGetComponents([UIColor colorWithRed:0.5 green:0.5 blue:0 alpha:1].CGColor)); // don't make color too saturated
CGContextFillRect(context, rect); // draw base
[[UIImage imageNamed:#"someImage.png"] drawInRect: rect blendMode:kCGBlendModeOverlay alpha:1.0]; // draw image
}
In the rect variable pass the area of the image you would like to tint

Erase a rect of an UIImageView

I want to know, how can I erase a custom rect (with, for example, an UIView in IB or something else) of an UIImageView in order to display an other UIImageView positioned underneath.
I didn't manage to do it using some response in the forum...
This will clear a rect in an image:
- (UIImage *)clearRect:(CGRect)rect inImage:(UIImage *)image {
if (UIGraphicsBeginImageContextWithOptions != NULL)
UIGraphicsBeginImageContextWithOptions([image size], NO, 0.0);
else
UIGraphicsBeginImageContext([image size]);
CGContextRef context = UIGraphicsGetCurrentContext();
[image drawInRect:CGRectMake(0.0, 0.0, [image size].width, [image size].height)];
CGContextClearRect(context, rect);
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
Just load your image and clear the rects before assigning it to the image view:
UIImage *image = [UIImage imageNamed:#"Image.png"];
UIImage *maskedImage = [self clearRect:CGRectMake(10.0, 10.0, 50.0, 50.0) inImage:image];
[imageView setImage:maskedImage];
probably not the best solution but you can do the other way and take the 4 parts around the rect separately and combine them afterwards without the inner rect. You would repeat this as long as you have rect's to crop out.
You cannot clear the UIImageView itself because this just draws the UIImage. So you have erase the rect in the UIImage. Create a bitmap context, draw the image into it. Erase the part you want to "see through" with CGContextClearRect. When create a new new image from the bitmap context.

Drawing a PNG Image Into a Graphics Context for Blending Mode Manipulation

I need to draw a series of PNGs into a CGContext so I can blend them together:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetBlendMode(context, kCGBlendModeOverlay);
Currently I have my app working based on just drawing each image as a UIImageView and adding them as a subview to the view file via: [self addSubview:someImageView] ...but this doesn't allow for blending mode manipulation, right?
Currently the UIImageView vars are being assigned via:
someImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image-name.png"]];
So I've tried replacing those with either of these with no luck:
[[UIImage imageNamed:#"image-name.png"] drawInRect:rect blendMode:kCGBlendModeOverlay alpha:1.0];
and this one needs a CGImage which I'm not sure how to make with the PNGs:
CGContextDrawImage(context, rect, someCGImage);
So what am I missing? These aren't being triggered by any interaction, they just need to load/draw when the app loads.
Thanks for any leads. :)
You don't need to be using UIImageViews.
Instead, bracket your drawing between calls to UIGraphicsBeginImageContext and UIGraphicsEndImageContext.
For example (code not tried):
UIImage* uiimage = [UIImage imageNamed:#"image-name.png"];
UIImage* uiimage2 = [UIImage imageNamed:#"other-image-name.png"];
CGSize size = uiimage.size;
UIGraphicsBeginImageContext(size);
[uiimage drawAtPoint:CGPointZero blendMode:kCGBlendModeOverlay alpha:1.0];
[uiimage2 drawAtPoint:CGPointZero blendMode:kCGBlendModeOverlay alpha:1.0];
UIImage* blendedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
If you want to use CG calls, call:
CGContextRef context = UIGraphicsGetCurrentContext();
to get the context ref.

How to get a color image in iPhone sdk

I want something as follows
UIImage *solid = [UIImage imageWithColor:[UIColor darkGrayColor]];
to create an image with respect to some color.
how to do it in iPhone sdk.
You can draw the color into a CGContext and then capture an image from it:
- (UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)size {
//Create a context of the appropriate size
UIGraphicsBeginImageContext(size);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
//Build a rect of appropriate size at origin 0,0
CGRect fillRect = CGRectMake(0,0,size.width,size.height);
//Set the fill color
CGContextSetFillColorWithColor(currentContext, color.CGColor);
//Fill the color
CGContextFillRect(currentContext, fillRect);
//Snap the picture and close the context
UIImage *retval = UIGraphicsGetImageFromCurrentImageContext(void);
UIGraphicsEndImageContext();
return retval;
}
If you're just trying to create a solid rectangle of colour why not just do something like
UIView *solid = [[UIView alloc] initWithFrame:someFrame];
solid.backgroundColor = [UIColor greyColor];
And then add the view to whatever subview you want to show the solid colour.
(That is, of course only if that's what you're trying to achieve. Maybe you aren't)

create UIImageView with portion of image file

I'm subclassing UIImageView to create a tile-based application. Essentially, I am taking a single image file and breaking it up into pieces, and then assigning the pieces to my tiles (UIImageViews), so that they can be manipulated independently.
What's the best way to grab a portion of an image and use that to draw a UIImageView? I thought about overriding drawRect and using CGAffineTransform, but it seems like there ought to be a simpler way to do this, perhaps by specifying a CGRect to the UIImage that is passed to the UIImageView, but I don't see an API for this.
Here we go:
UIImage* img = [UIImage imageNamed:#"myImage.jpg"];
CGRect imgFrame = CGRectMake(x, y, tileWidth, tileHeight);
CGImageRef imageRef = CGImageCreateWithImageInRect([img CGImage], imgFrame);
UIImage* subImage = [UIImage imageWithCGImage: imageRef];
CGImageRelease(imageRef);