Transparent UIImage - iphone

I am creating an iOS application in which I have added a simple UIImage. I want to add the transparency effect in image to show other images behind that main image. Tell me how I can achieve this effect?
Note: I don't want to change the opacity/alpha of image.

An alpha change is how you alter transparency. In addition to this, you would want to alter this on the view level not on to the UIImage directly. E.x:
[myImageView setImage:[UIImage imageNamed#"myImage.png"]];
[myImageView setAlpha:0.7f];

use images in png format with alpha channel in it: alpha channel is the layer that tells how many transparent each pixel is

how about this:
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f,0.0f,1024.0f,768.0f)];
self.imageView.backgroundColor = [UIColor colorWithRed:255 green:255 blue:255 alpha:0.5];
[self.view addSubview:self.imageView];
or set its alpha
imageView.alpha = 0.5;
But be sure to instantiate the imageView, as property then synthesize.

Related

How to fill the part of the imageview with another color

I am developing an application in which I am using one UIImageView and adding one rectangle image to that UIImageView. I am getting some float value from another calculation like 67.4 and I need to place one green image on the 67.4 part of rectangle UIImageView. So please tell me how can I fill that green image onto that UIImageView ?
You can make a "rectangle", i.e. a UIView and color it as you wish. Then add it to your image view.
UIView *greenRect = [[UIView alloc] initWithFrame:
CGRectMake(67.4, 67.4, width, height)];
greenRect.backgroundColor = [UIColor greenColor];
[imageView addSubview:greenRect];

How to set background image for a View?

the default background color is white and I want to change it to an image
I have tried this:
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]];
but the background turn black, the image is not at the background. I don't know why.
by the way if I have more than 1 image in one view, how to set their priority of display?
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"imageone"]];
backgroundView.frame = self.view.bounds;
[[self view] addSubview:backgroundView];
Try this out, this is calling self.view.bounds for the frame instead of self.view.frame for frame. I just tested it and it works perfectly.
EDIT: If all you're looking for is to send the image to the back of the stack, you can do the following and select send to back.
Code for you :
UIImageView *myImage = [[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"background.png"]]autorelease];
myImage.frame = self.view.frame;
[self.view addSubview:myImage];
You should to use a UIImageView and its frame is uiview's frame.

iPhone: Mask UIImageView of differing dimensions into square dimension

I have a bunch of UIImageViews that are in different proportions. Some of 100x101 some are 130x121.
How can I mask these to 80x80 and NOT stretch the images? I basically just want to mask a square out of each one. (kind of like the Apple's Photo thumbnail view does)
Set the image view's size to 80 x 80
set the image view's contentMode property to UIViewContentModeScaleAspectFill
Finally, to make round corners, use the following code, and import QuartzCore/QuartzCore.h at the beginning of your implementation file.
CALayer * layer = [myImageView layer];
[layer setMasksToBounds:YES];
[layer setCornerRadius:12.0f];
Edited: Yes, by saying size I mean frame, the W and H:
Set its content mode UIViewContentMode, you may be looking for UIViewContentModeScaleAspectFit or UIViewContentModeScaleAspectFill.
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
[imageView setContentMode:UIViewContentModeScaleAspectFit];
[imageView setImage:[UIImage imageNamed:#"myImage.png"];
.
.
.

"Masking" an animation? iPhone SDK

I have been looking into ways of masking images on the iPhone. I came across this solution
http://iosdevelopertips.com/cocoa/how-to-mask-an-image.html
which works great for still images. What I want to do is mask an animation in a UIImageView. From what I have read I don't think this is possible whilst also achieving a decent frame rate.
This leads me to ask whether the following is possible, could I "clip" the images inside the UIImageView? ie not re-size the UIImageView to the size of the images so some parts are chopped off?
I haven't tried this for performance, but you can use a Core Animation layer as a mask. You can define a path to use in a CAShapeLayer and fill the shape. Then specify the layer mask of your UIImageView's layer. Something like:
CGMutablePathRef path = CGPathCreateMutable();
// build the path by adding points
// ...
CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
[shapeLayer setPath:path];
[shapeLayer setFillColor:[[UIColor blackColor] CGColor]];
// Set shape layer bounds and position
// ...
// Set the mask for the image view's layer
[[imageView layer] setMask:shapeLayer];
Keep in mind that this isn't actually creating a new image as the link you reference does. This just creates a mask for display over top of your image view--which may not be what you want.
I searched high and low and finally found a solution with practically no limitations at all. So, here you go:
UIImageView *maskeeImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"MaskeeImage.png"]];
[maskeeImage setAnimationRepeatCount:-1];
[maskeeImage setAnimationImages:[[NSArray alloc] initWithObjects:[UIImage imageNamed:#"MaskeeImage1.png"], [UIImage imageNamed:#"MaskeeImage2.png"], [UIImage imageNamed:#"MaskeeImage3.png"], nil]];
[maskeeImage startAnimating];
CALayer *maskeeLayer = [maskeeImage layer];
maskeeLayer = CGRectMake(0, 0, 768, 1004);
[[[self view] layer] addSublayer:maskeeLayer];
UIImage *maskImage = [UIImage imageNamed:#"ImageMask.png"];
CALayer *maskLayer = [CALayer layer];
maskLayer.contents = (id) myImageMask.CGImage;
maskLayer.frame = CGRectMake(0, 0, 768, 1004);
[maskeeLayer setMask:maskLayer];
There you go! It's actually really easy, once you know how. I tried to show a few different options; Using UIImageViews or UIImages, Animations (Which can also be used for the mask).
To sum it all up, you basically have to set the mask property on your view's CALayer. Every UIView subclass has a CALayer attached to it, so you aren't restricted at all in terms of where you get your mask or maskee from.
Hope this helped. Cheers, Dylan.

How to let the text in a UILabel appear blurry?

Is there a way to achieve a blurry or glowing effect for the text? Or would I have to fetch the graphics context of the layer and then apply some kind of gauss algorithm on the pixels? I searched the documentation on this but it appears that shadows don't draw blurry and there's no method in NSString, UIFont or UILabel that could help to do it.
CGContextSetShadowWithColor can be (ab)used to draw blurred text. As it can be slow, it is best to draw it to a buffer and then reuse that:
UIFont *font = [UIFont systemFontOfSize:18.0f];
UIGraphicsBeginImageContext(CGSizeMake(200.0f, 50.0f));
CGContextSetShadowWithColor(UIGraphicsGetCurrentContext(), CGSizeMake(0.0f, -500.0f), 2.0f, [[UIColor blackColor] CGColor]);
[#"Blurred Text!" drawAtPoint:CGPointMake(5.0f, -500.0f) withFont:font];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[imageView setImage:image];
Old answer:
A simple option is to draw the text a number of times and offset the x/y coordinates by 1 each time. To speed it up you can cache it on an offscreen buffer.
You can intentionally position the label on a half pixel and you'll get a blurry effect. You won't have much control over it, but it will look blurry:
UILabel *labelOnWholePixel = [[UILabel alloc]
initWithFrame:CGRectMake(20.0f, 20.0f, 280.0f, 25.0f)];
[labelOnWholePixel setText:#"Whole Pixel"];
[labelOnWholePixel setBackgroundColor:[UIColor clearColor]];
UILabel *labelOnHalfPixel = [[UILabel alloc]
initWithFrame:CGRectMake(20.5f, 50.5f, 280.0f, 25.0f)];
[labelOnHalfPixel setText:#"Half Pixel"];
[labelOnHalfPixel setBackgroundColor:[UIColor clearColor]];
[[self view] addSubview:labelOnWholePixel];
[[self view] addSubview:labelOnHalfPixel];
I'm not sure, but it appears to me that the closer you are to the whole number in either direction, the clearer it gets, so you can control it a little.
Use photoshop or some other image editor to create a clear, blurry overlay. Save as a PNG. Load this PNG into an UIImageview and draw it on top of the UILabel. Adjust the alpha to alter the effect.
You could even create a specialized UILabel subclass to handle this with several degrees of blur.
Aside from antialiasing UILabel does not support blurring its text (or its shadow). You'll have to code it manually.