Magnifier effect with CALayer s - iphone

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

Related

Hide / reveal UIImage based on finger path?

I hope that makes sense. I'll try to explain it.
I have a UIImageView on screen, and am wondering how I can take the area after "drawing" on it with a finger, and remove that section from the UImage, or, create a separate UIImage from the selection.
I'm not looking for code (unless you have it =] ), just an idea of how to go about doing it. If you have tips, I'd be very grateful, thanks.
If I understand your question,
I think I would add a transparent UIView as a subview over the top of the UIImageView. And draw on that. Then you can remove/hide the subview when your done.
you need to create a UIGestureRecognizer with target and a action like -imageIsPressed, in this -imagePressed method you can call something to make the image disappear. I would suggest placing the UIImage into a UIImageView and calling imageview.hidden = YES; to hide the image, and set it back to "NO" once its not held by the finger.
You'd need to implement something that captures the area the user 'selected' (maybe be creating a CGPath. Then you create a CALayer of the size of the imageView. In it you create, draw and fill the captured path with some arbitrary color while leaving the rest transparent. Finally you apply your generated CALayer as a mask to the UIImageView:
imageView.layer.mask = maskLayer;
Hope that gets you started.
For more info on how to draw that custom CALayer pls refer to Quartz Programming Guide
So basically you want to implement freehand erasing of an image? You will need to use core graphics and the various CGContext methods (with blend mode set to clear) to achieve this. There are two approaches, but both start with drawing your image as the first part of drawRect, and then
1) Store your strokes in an array, and stroke all of them over top of the image.
2) Stroke one stroke over the image and then store the resulting image into a UIImage. Use this UIImage as the next image that you draw in drawRect. This one is difficult for undo/redo functionality.
I recently implemented this myself and made the source available here. Basically I used the same methods described here with this change when setting up the graphics context:
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);

Embedding a UITextView in an image

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.

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.

Zoom Loupe on UIImageView

Does anyone know of a way to implement the zoom loupe functionality of a UITextField on the iphone in a UIImage view?
Part of the app I'm building allows a user to draw a line on a UIImage, a process that might involve precision positioning of the points. In order to help the user, I want to provide the zoom loupe as seen when positioning the cursor in a UITextField. Does anyone have any idea as to how to do this? Any pointers to relevant docs?
Cheers!
This tutorial is well written and includes sample code:
http://www.craftymind.com/2009/02/10/creating-the-loupe-or-magnifying-glass-effect-on-the-iphone/
You need to comment out the line that imports CustomView.h and then it compiles and runs fine.
I do not know of any relevant docs.
What I would do when implement something like this is. Show a UIImage representation of your view over the view. Then clip the overlay UIImage to just the portion of the view you want to enlarge. You can enlarge the portion by using CGAffineTransformScale.
You can draw a existing view to a UIImage by using CALayer's renderInContext: method.
UIGraphicsBeginImageContext(self.view.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:context];
UIImage *imageRepresentation = UIGraphicsGetImageFromCurrentImageContext();

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