I am completely new to implementing custom drawRect method (and Core Graphics) but am doing so to improve the scrolling performance for my UITableView. Please do let me know if I am doing anything stupid.
In my cell, I have a UIImage and over the bottom part of it I would like to print the caption of the image. However, in order for the caption text to show up clearly regardless of the image , I would like to have a black rectangle with opacity of ~75% on top of the UIImage and below the caption text.
I tried the following
[self.picture drawAtPoint:point];
[[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.75] setFill];
UIRectFill(CGRectMake(rect));
but that resulting fill actually eat into the UIImage (excuse my poor description sorry) and the part showing below the slightly transparent fill is the background of my UITableView…
I guess I could have made another image for the rectangle and then draw it on top of the self.picture but I am wondering whether this is an easier way to use UIRectFill to achieve this instead…
as mentioned, I am completely new to Core Graphics so any hints would be much appreciated. thanks in advance!
Also, I have a second question… the dimension (in pixel) of the image downloaded is twice that of the rect (in points) that it will fit in, to account for retina display. However, it is now currently going over that rect, even on an iPhone4 device… How can I fix that (including for pre-iPhone4 devices too?)
I don’t do much custom
drawRectstuff, so I’ll defer that portion of the question to someone else, but usually tableview performance issues are solved much more easily by moving the expensive calculations into a background queue and then asynchronously updating cell from the main queue when that background operation is done. Thus, something like:First, define an operation queue property for the tableview:
Then in
viewDidLoad, initialize this:And then in
cellForRowAtIndexPath, you could then:You can optimize this further by making sure that you cache the results of your computationally-expensive stuff into something like a
NSCache, and perhaps toDocumentsor elsewhere as well, thus as you can optimize how often that complex stuff has to be done and really optimize the UI.And, by the way, when you do that, you can now just have your
UILabel(withbackgroundColorusing thatUIColorfor black with 0.75 alpha) on top of the theUIImageView, and iOS takes care of it for you. As easy as it gets.On the final question about image resolution, you can either:
contentScaleFactorto figure out whether you’re dealing with retina or not and resize the thumbnail image accordingly; orcontentModeofUIViewContentModeScaleAspectFillwhich will make sure that your thumbnail images are rendered correctly regardless … if you’re using small thumbnail images (even 2x images), the performance is generally fine.