本文介绍了如何绘制UIImage或直接在-drawRect中:?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIImage ,我想在 UIView 上绘制。但是,我不想创建 UIImageView 并将其添加为子视图,而是要覆盖 -drawRect:并绘制UIView

   -  

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

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

//如何直接绘制到视图中?
}


解决方案

致电 [img drawInRect:rect];


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 -drawRect: 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];.

这篇关于如何绘制UIImage或直接在-drawRect中:?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 22:30