开发者

How to draw an UIImage or directly in -drawRect:?

开发者 https://www.devze.com 2022-12-10 13:17 出处:网络
I have an UIImage which I want to draw on a UIView. But instead of creating an UIImageView and adding this as a subview, I want to overwrite -drawRec开发者_JS百科t: and draw my UIView directly.

I have an UIImage which I want to draw on a UIView. But instead of creating an UIImageView and adding this as a subview, I want to overwrite -drawRec开发者_JS百科t: and draw my UIView directly.

For example, my code looks like:

- (void)drawRect:(CGRect)rect {
    CGContextRef myContext = UIGraphicsGetCurrentContext();

    UIImage *img = [UIImage imageNamed:@"foo.png"];

    // how to draw the directly into the view now?
}


Call [img drawInRect:rect];.


BTW, you shouldn't load the image file in the drawRect method. Because the method is called whenever the view is required to update. Therefore, it (of course, loading procedure) may be called many many times during running. (Furthermore, OS2.x's imageNamed method has a bug -- not cached the image and leaked it.)

Therefore, you'd better to load the file in the initialization method, not in the drawRect.

0

精彩评论

暂无评论...
验证码 换一张
取 消