Removing image background in UIImageView at runtime - iphone

I have a ball assigned to a UIImageView in Interface Builder. An IBOutlet from the UIImageView is wired to a corresponding UIViewController. The image has a white background. When I assign it to the UIImageView in IB, the background is transparent. In IB, I have the UIImageView set to a transparent background and aspect fill.
When I assign the UIImageView an image at runtime:
self.ball.image = ballImage; //ballImage is a UIImage
self.ball.backgroundColor = [UIColor clearColor];
self.ball.contentMode = UIViewContentModeScaleAspectFill;
the UIImageView square has a white background where the ball doesn't display. Meaning all four corners. What is the difference that the IB version doesn't show a white background at runtime and the programmatic version does?

Make sure you set self.ball.opaque = NO; in addition to setting the background color to clear. Otherwise, a white background will still be drawn. I believe you have to set both of these whether you use IB or Xcode to create the view - but IB may have set them both for you.

If you need to remove background programmatically you may refer to "Bitmap Images and Image Masks" guide. I have no other idea on this point...just using CGImageCreateWithMaskingColors.
In case your *.PNG (or any graphic resource file) doesn't have built-in transparency (just white background) but with IB it looks like transparent - may be alpha property less then 1 for your UIImageView and you see partially transparent image.

Related

UITableViewCell - How to set selection clipping?

In my app, I have a UITableView and each UITableViewCell utilizes a custom background and style with the following code in the "WillDisplayCell" method:
UIImage *cellBackgroundImage = [UIImage imageNamed:#"TableView_Cell_Background_iPhone"];
UIImageView *backgroundImage = [[UIImageView alloc] initWithImage:cellBackgroundImage highlightedImage:cellBackgroundImage];
[cell setBackgroundView:backgroundImage];
[backgroundImage release];
The problem is that the PNG I'm using as the background is a rectangle, but the UITableViewCell is a rounded rectangle with a specific layer radius property. When I select a cell, the background overrides the rounded nature of the cell and I get a jarring blue highlighted sharp rectangle. Is there a way to set the selected state corner radius or something along those lines? My only other option if not would be to create a PNG background that fits the rounded rectangle perfectly.
Thanks.
I would set the selectionStyle of the cell to UITableViewCellSelectionStyleNone, this stops the blue highlight. Then start to do a custom selection job.
You will have to make a custom subclass of UITalbeViewCell. Override setSelected or if that doesn't work user a gesture recognizer or override touchesBegan etc. I have done it once, before, I forget exactly how, if you have trouble let me know and ill look it up.
When you detect that the cell is selected, perhaps create a translucent overlay as a PNG and make it appear when the cell is selected. Alternatively lower the transparency of the background image or add a color mask.

UIImageView not showing the background View if the image has transparent regions

I have a UIView has some labels and buttons on it.
Next I also have a image which has a square area that is transparent, now
if I create a UIImageView and add this image which has transparent regions I am not able to see the background view (which has buttons and labels) through this transparent image.
If I play with the alpha value that doesn't work as intended which is to see the transparent regions exactly as it would have appeared on the UIView which has the labels and buttons.
UIImage* image = [UIImage imageNamed:#"TI1.jpg"];
UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
Also I would be interested to know if there is other way to achieve what I am trying to achieve.
Basically I want to highlight a certain area of the view which has buttons/labels and make the rest of the area greyed out. My idea was to have this UIImageView with transparent regions in image to achieve that.
Thanks
Ankur
Try setting imageView.opaque = NO;
UIImageView inherits from UIView. According to that class's docs:
This property provides a hint to the drawing system as to how it
should treat the view. If set to YES, the drawing system treats the
view as fully opaque, which allows the drawing system to optimize some
drawing operations and improve performance. If set to NO, the drawing
system composites the view normally with other content. The default
value of this property is YES.
Also, not sure that JPG even supports transparency, so try exporting the image as a PNG to ensure you get the results you're looking for.

How do I set a UIView image UIImageView to the background?

I have a custom view for which I've (in the XIB) created a UIImageView as a subview. How can I make the image view appear in the background? (i.e. so that in the custom view I can create the hands of a clock that will appear over the top)
Do I need to make my background image have alpha areas for transparency? (if I have the terms correct) Or is it OK to have an image with it's background just set to white or black or whatever it needs to be (on the basis it will be in the background so this will be ok)
You can't: views are composited on top of their superviews, which means if your custom view is drawing in -drawRect:, it will always appear under the UIImageView.
The easiest way to achieve the effect you're looking for is to put the image view under your custom view in IB.
Background images generally do not need to have transparency (and there's a slight speedup if the PNG has no alpha channel).
An alternate approach to using UIImageView objects if you are drawing the rest of stuff in drawRect: is to use the image directly. Draw the image in drawRect: before you do any of the drawing.
Example
- (void)drawRect:(CGRect)rect {
[backgroundImage drawInRect:rect];
/* Do rest of your drawing */
}
Now this is only if you are using a complex image. If it is a texture, you would rather use UIColor's colorWithPatternImage: method to get a color from the UIImage object and set your custom view's backgroundColor. Remember that using the colorWithPatternImage: will not scale the image while drawing and will tile if the source image is smaller the frame of the view.

How to show UIImage background through transparent pixels of image

I have an image, that has transparent areas and a known backround. But wheh I set "image" property of the UIImage i see main view and not a color which i set in "background" property.
I think I tried that before and found no real solution. You could however put the uiimageview into an uiview with the same size and set the backgroundcolor to this view.
Had to create custom control with two UIImageViews bounded by obu UIView. It works, but i would prefer apple doing something about this behaviour

Why does UIImageView appear to be partially transparent

I have a simple app that just displays a UIImageView. For some reason the image appears that it is partially transparent as it is much lighter than if I view the same image in photoshop. Any suggestions...I though by default a UIView is opaque.
You have probably changed the alpha of your imageView or the view itself. check for the the transparency of your imageview also. Because the default alpha of the imageview is 1 and opacity is also set to 100%. You might have chane one of these otherwise this is the problem of your image only
I don't believe that UIImageView is by default partially transparent? Try changing the background colour of the application and see if that affects the outcome of your image, if it is transparent it should be darker with a black background rather than white.