Embedding a UITextView in an image - iphone

I have a UIImageView displaying a picture that I took with the iPhone Camera.
On top of that, I have a UITextView in which the user can type stuff that describes the picture.
I would like to create a combined image of the two views and send it to a server.
Anyone knows how I can do that (just creating the image. I know how to send it)

If the UIImageView and UITextView are contained in another UIView. Or if you can create a UITextView and UIImageView on the same UIView you can then do this.
UIGraphicsBeginImageContext(myView.bounds.size);
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Where myView is equal to the UIView containing the two.
You can also draw each one separately (by setting the frame appropriately) into myView to accomplish the same thing.

Related

Crop UIImage to fit a frame image

I need to crop a UIImage, taken with the device camera, so that it fits inside another UIImage, which represents a frame (with rounded borders and so on). Check the image below:
Using Aspect Fill
Using Aspect Fit
So, what I need is to remove the image excess that is out of the frame bounds.
I tried using UIBezierPath, CGImageRef and other methods that I Googled but I'm not finding a solution for this.
In Interface Builder, use the following configuration:
There are two important settings, namely:
Mode: Aspect Fill
Clip Subviews
It can also be done programmatically:
[imageView setContentMode:UIViewContentModeScaleAspectFill];
[imageView setClipsToBounds:YES];
This will correctly fill the view with the image, keep its aspect ratio and hide what doesn't fit.
make a IBOutlet in your controller.
#property (retain)IBOutlet UIImageView* imageView;
and in -(void) viewDidLoad set
imageView.layer.masksToBounds = YES;
In interface Builder, access the Mode menu inside of the detail pane (the fourth one) and choose the right one for your UIImageView (I guess "center" or "aspect fit").
OLD ANSWER:
You can use the contentGravity property of CALayer to make it work
A constant that specifies how the layer's contents are positioned or scaled within its bounds.
#property(copy) NSString *contentsGravity
Use an UIImageView and do
imageView.contentMode = UIViewContentModeScaleAspectFit;

How to enlarge/blow up/zoom a certain part of a UIImageView on iPhone?

I have a magnifying glass as a UIImageView subview of a UIView. I want to enlarge the part of the UIView that the magnifying glass is on. I can't figure out how to enlarge a certain part of the view. I assume it has to be a rectangle and not a circle? How could I blow it up? I don't have much CA experience so a shove in the right direction would help.
If you want to zoom a static image, then you can do this:
Make the magnifyingView (UIView) have a sub-view that's a UIImageView. The UIImageView has the same image that's being magnified, but much larger. Have the magnifyingView clip to bounds set. Then, as the UIView is moved by the user dragging/touching, the subview is moved in relation to that to create a magnify effect.
I'd have to look up how to magnify dynamic content myself.
I am about to do the same thing. Here is an example of how to capture a view in an image. You can then zoom in the image in the bubble:
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
self.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

what is the difference between UIImageView and drawInRect?

I want to display so many images in table cells. I knew two methods to show an image.
One is creating an instance to UIImageView and show it
CGRect rect=CGRectMake(x,y,width,height);
UIImageView *image=[[UIImageView alloc]initWithFrame:rect];
[image setImage:[UIImage imageNamed:#"sample.jpg"]];
Another method is,
CGRect rect=CGRectMake(x,y,width,height);
[[UIImage imageNamed:#"sample.jpg"] drawInRect:rect];
Now, my question is, what is the difference between these two? Which one is efficient? Or someother function is available better than this?
Thanks in advance....
Using the drawInRect: method has CoreGraphics draw the image into the active CGContext using the CPU. Assuming you're in a UIView's drawRect: method, this will paint the image into the view's buffer.
UIImageView uses whichever image is assigned to it as it's backing buffer instead of using the slower drawRect:. The GPU then references this buffer directly when the screen is composited via QuartzCore.
UIImageView is a UIView subclass. By adding it to the view hierarchy you get all the free benefits: animations, size properties, affine transoforms, etc. Plus, you are able to change the underlying UIImage any time you want.
On the other hand, calling drawInRect: will just draw the image where you tell it to, but not interact with the UIView hierarchy, so you don't gain any of the benefits from having it on the view hierarchy.
I would think (have not tried it), that drawing the image directly is faster, but I would think in most of the cases having a UIImageView is a better idea.

Magnifier effect with CALayer s

I want to implement a magnifier exactly like the one is shown when an UITextView is long pressed.
I got the idea from here: iPhone, reproduce the magnifier effect
But I am working only with CALayers not UIViews, hence I don't have a drawRect method to write in. I wonder where should I write this?
inside display method? or drawInContext: method?
How can I efficiently raster all the layers from the original view? (the view to be magnified) is it really a good idea to do:
UIGraphicsBeginImageContext(magnifyView.bounds.size); //magnifyView is the view to be magnified
[magnifyView.layer renderInContext:UIGraphicsGetCurrentContext()];
_cache = UIGraphicsGetImageFromCurrentImageContext(); //_cache is an UIImage
UIGraphicsEndImageContext();
and then get the portion I need from this UIImage's CGImageRef?
Thanks

UIImage and UIImageView problem

set a UIImageView.image property to a UIImage is unable add the image to the view,however if i set the imageview frame property,it works.can anyone tell me how it's happen?
and what will happen when i apply the UIImage instance method[drawInRect:] redraw the image frame which is larger than the view frame?i have tried but nothing happen,and what is this function doing actual?
Generally, how it works is, you add an Image to a UIImageView and then add the UIImageView to a UIView to display it on the screen. You can do this programmatically, or using Interface Builder. Optionally, you can create a CGRect (based on a frame, if you like), use this as the bounds for the UIImageView (or you can frame the UIView). There are several ways to do what you want to do.
It's hard to tell without seeing your code. It could be a number of things. You could be inserting the view with the image behind the currentView? You may not be retaining the UIImageView. We can't read minds here necessarily, (sometimes we do). But you'll need to be more helpful if you want a solid answer.
Or just take a look at this:
UIImageView Docs