what is the difference between UIImageView and drawInRect? - iphone

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.

Related

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.

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

how to draw an UIImageView into -drawRect of another view?

I have several UIImageView objects which have been rotated and scaled. Because they are just background images, and represent an non-highlighted-state, I want to draw them to the canvas of the view so that I can get rid of them in memory. The view has a big bitmap anyways, so it would save a lot of memory to put them in there rather than adding as subview.
It seems I can only call something like -drawInRect for an UIImage, but how about an UIImageView with transforms on it? Oh yes, and it's positioned with frame origin.
I just want to draw it to another UIView's bitmap the same way as it appears when adding as subview.
Using drawInRect: with a rect of different origin and size of the image, you can effectively make any scaling and translation you want. You can also handle rotations with the [CGContextRotateCTM][1] function (CTM = current transformation matrix). There are other CGContext..CTM functions as well, so you have multiple approaches.

Setting background colour of a UIView increasing memory usage

I am loading a number of UIViews onto a UIScrollView and am trying to track down why they are using so much memory. After a process of elimination using Instruments I have discovered that setting the background colour of the views increases memory usage by 4 times.
If I don't set the background colour the memory usage sits at around 4.5megs. As soon as I set the background colour to anything redColor or clearColor the memory usage jumps to 17megs.
Here is the code:
ThumbnailView *thumbView = [[ThumbnailView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 225.0f, 320.0f)];
thumbView.tag = aCounter;
thumbView.backgroundColor = [UIColor clearColor];
Does anyone know what could be causing this?
What I am really after is to have the background clear. If there is no way around this, is there another way of setting the background of a UIView to be clear?
All instances of UIView (and classes derived from it) have an associated CALayer object (accessed via the layer property) that provides the UIView's visual appearance. The CALayer can have it's own bitmap, it can share a bitmap with another CALayer object (which is how reflections are done), or it can have no bitmap.
When a UIView acts as a container for other controls, it has no bitmap associated with it's layer, thus it uses very little memory. As soon as you set it's background colour, that backing bitmap has to be created so that there is something to render. If the UIView subclass implements drawRect to draw some graphics into the view, the same thing will happen.
Because a full-screen sized view consumes a lot of memory, when you implement a UIScrollView based solution, you should only load the views that are displayed and the two either side. Don't create loads of them in advance.

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