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

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.

Related

Creating translucent UIView with gaussian blur

I am in the process of creating an app where I have a background image (a UIImageView with the frame set to the UIView's bounds and added as a subview).
I would like to now add a UIView that will be over the background image but will apply gaussian blur to the image behind, only over the area where the UIView is.
I have just tried to use GPUImage to create a blur, which looks good but it uses 2 UIImageViews (one large background and one behind the UIView, which is blurred). Unfortunately the image no longer matches up with the background.
Is there any way to get the UIView to blur only the area behind it? A bit like the UIView is frosting the image behind.
Thanks!
Try FXBlurView from here - https://github.com/nicklockwood/FXBlurView
It should work.

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 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

How to erase particular portion without effecting other part in UIImageView?

I have one UIView with an UIImageview in which i have an image downloaded from web service. Now i try to draw with drawRect: in it. I want to erase part of my drawing same as like eraser without effecting background image downloaded from web service.
So how can i do that?
Thanks
If you simply want to "replace" part of the UIImageView with rendered primitives, rather than editing the image data, just place a UIView with a [UIColor clearColor] background color on top of the UIImageView and draw into that overlay UIView with a non-transparent color.
I don't think your looking at it the right way.
How I would do this is:
1) get the bitmap in the UIImageView
2) change the pixels/areas that you want to change
3) set the altered bitmap in your view

How do I blur whatever is behind a translucent UIImageView?

I have a translucent png image (made by turning down the opacity in Photoshop), and there's a UIImageView behind that translucent image. Is there any way to blur the parts of the UIImageView on the bottom that intersect with the UIImageView on top? Thanks!
Not aware of any code that will do that on its own, but you could 'fake it' by rendering the intersection of the views in a graphics context, then apply a blur effect to it. Then you could take the graphics context and insert a UIImageView with the resulting image in between the two views to simulate the effect. This method would not work very well if you have any sort of animation or otherwise changing the state of the images.
You may try setting alpha of the image to less than 1.