Draw random image on a bubble image background like iMessage - ios

This is a chat app, the bubble is the background image, for text, i can just stretch the bubble image with "resizableImageWithCapInsets", and draw the text on the bubble.
But problem is when it is a random image, how can i draw the image on the bubble the way it is in the attached picture? both ios message and imessage on mac show the image in this way.
http://i.stack.imgur.com/sgJhv.png

If your bubble is represented by a UIImageView (which is a subclass of UIView), you can add another UIImageView as a subview of your bubble. You would make the frame of this subview equal to the frame of your bubble, and then set the bubble's clipsToBounds property to YES, so the subview is constrained to the bubble.
You would then set the contentMode of the subview to UIViewContentModeAspectFill, which will strech the image to the size of your bubble. Something like this:
//After resizing the bubble to the size you want, to this:
bubbleView.clipsToBounds = YES;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:bubbleView.frame];
imageView.image = theImage;
imageView.contentMode = UIViewContentModeAspectFill;
[bubbleView addSubview:imageView];'
[imageView release];

Take a look at BubbleThingie sample app. It does the image masking and gloss effect like in your example image.

Related

Get uiview section under uiimageview?

I have an UIView as background, and a UIImageView above it.
What I want to do is fill the UIImageView with the UIView section that is in the back (without the white border)
I tried cropping a snapshot of the background but it doesnt look good. there is always a difference.
Make the background UIView a subview of the UIImageView and then set the property of the UIImageView yourImageView.clipsToBounds = YES
Use CALayer mask. The mask will be the smaller image view, and will be assigned to the background view's mask property.

UIImageView - Stretch to fit issue

I have an app which requires dynamic background images (they are set server side) - the views used are both UIViewControllers and UITableViewControllers, so I have created classes which extend both controller type and add a background image to each view on load.
All works fine apart from the background image is refusing to stretch on an iPad on the UIViewController only - I am using Xcassets and have the standard and #2x retina images applied - all works fine on iPhone - and on both for UITableViewControllers but the UIViewController class doesn't appear to stretch the background on an iPad - it simply displays the image in the top left corner at the standard 320 width size.
This is my code - can anyone suggest where I am going wrong?
UIImageView *backgroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed: [self.prefs stringForKey:#"BGImageBlur"]]];
backgroundImage.contentMode = UIViewContentModeScaleToFill;
[self.view addSubview:backgroundImage];
[self.view sendSubviewToBack:backgroundImage];
You're creating a UIImageView and providing it the image to set within the view. This will create the UIImageView with the frame of the given Image. You must first specify the frame, then set the image. Try this:
UIImageView *backgroundImage = [[UIImageView alloc] initWithFrame:self.view.bounds];
backgroundImage.image = [UIImage imageNamed:[self.prefs stringForKey:#"BGImageBlur"]]
This will create an ImageView that is the same size as its parent. If this is not what you want you'll probably have to specify some other layout constraints.

Can UITableViewCell´s ImageView cover the whole background?

I´m build a swipe feature for my todo-list application. When a user swipes(drag) the cell horizontally I would like to display a background image that covers the gap between the screen edge and the cell edge.
When I add an image to the imageView-property it just follows the cell. Can I somehow fix it to cover the whole background?
It sounds like you want to make something like Clear.
What you will need to do is:
Subclass UITableViewCell
Add your image view like this..
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.bounds];
imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
imageView.image = myImage;
[self insertSubview:imageView atIndex:0]; //I'm not at a compiler, so I'm not 100% on this, but it's something like this.
Add a UIGestureRecognizer to handle gestures, and set up dragging and appropriate actions.

Overlaying UIView with an background image

Is it possible to overlay UIView with an image without using UIImageView from the Interface Builder?
If not, how would you accomplish the same in code?
From the UIView documentation:
Image-based backgrounds - For views that display relatively static content, consider using a UIImageView object with gesture recognizers instead of subclassing and drawing the image yourself. Alternatively, you can also use a generic UIView object and assign your image as the content of the view’s CALayer object.
Using the second option, to set someView to have a background of yourImage.png:
someView.layer.contents = (id)[[UIImage imageNamed:#"yourImage.png"] CGImage];
if by Overlying a UIView you mean painting its background with an image, it can be done like this:
someView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"yourImage.png"]];
You could also add a new UIImageView that covers your UIView
by code, Init your UIImageView with the same frame Rect as your UIView and add it as a subview.

UIView::addSubView obstructs the navigation bar originally at the top

I designed a very simple interface for an ipad device: UIView + a navigation bar.
Then after the view is load, it will download an image from a location and use the following method to display it:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
UIImage* testImg = [UIImage imageWithData:_networkData];
UIImageView* testView = [[UIImageView alloc] initWithImage:testImg];
[_view addSubview:testView];
[testView release];
}
The problem is now the new UIImage occupies the whole visible area of the Ipad.
I wonder how can I fix this issue? I suppose I have to find out the frame of the display area in the original view, and then assign it to the UIImageView instance?
initWithImage will automatically adjust the frame to match the size of the image you're passing in. To tell the UIImageView to take the same size as its parent view you could just add the following line before you add the subview:
testView.frame = _view.bounds
...we use bounds rather than frame because the superview may have an offset that we don't want the image view to have.

Resources