【发布时间】:2014-07-17 01:04:19
【问题描述】:
我创建了一个 UIView 类别来轻松绘制,而无需创建 UIView 的子类。 问题是,当我关闭应用程序并重新打开(通常在内存不足的情况下)时,UIView 绘图消失了。
我给你看分类的代码:
UIView+DrawBlock.h
#import <UIKit/UIKit.h>
// Blocks
typedef void(^DrawBlock)(CGContextRef context, CGRect drawFrame);
typedef void(^CompletionBlock)(UIView *view);
@interface UIView (DrawBlock)
- (void)drawInside:(DrawBlock)block withResult:(CompletionBlock)completion;
@end
UIView+DrawBlock.m
#import "UIView+DrawBlock.h"
#pragma mark - Auxiliar UIView
@interface DrawingView : UIView
@property (strong, nonatomic) DrawBlock drawBlock;
- (void)setDrawRectBlock:(DrawBlock)block;
@end
@implementation DrawingView
- (void)setDrawRectBlock:(DrawBlock)block {
_drawBlock = block;
if (_drawBlock) {
[self setNeedsDisplay];
}
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
if (_drawBlock) {
_drawBlock(context, rect);
_drawBlock = nil;
}
}
@end
#pragma mark - Category
@implementation UIView (DrawBlock)
- (void)drawInside:(DrawBlock)block withResult:(CompletionBlock)completion {
if (block) {
DrawingView *drawView = [[DrawingView alloc] init];
drawView.translatesAutoresizingMaskIntoConstraints = NO;
drawView.userInteractionEnabled = NO;
drawView.backgroundColor = [UIColor clearColor];
[self addSubview:drawView];
NSDictionary *views = NSDictionaryOfVariableBindings(drawView);
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[drawView]|"
options:0
metrics:nil
views:views];
[self addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[drawView]|"
options:0
metrics:nil
views:views];
[self addConstraints:constraints];
[drawView setDrawRectBlock:block];
if (completion != nil) {
completion(drawView);
}
}
}
@end
我认为问题出在这部分代码中:
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
if (_drawBlock) {
_drawBlock(context, rect);
_drawBlock = nil;
}
}
如果我评论这一行:
_drawBlock = nil;
似乎一切正常,但内存消耗不断增加,应用变得很慢。
有什么想法吗?子类不是一个选项。
谢谢!
更新 1 使用示例
- (void)drawOnView {
[self.view drawInside:^(CGContextRef context, CGRect drawFrame) {
// Oblique lines
CGMutablePathRef obliquePath = CGPathCreateMutable();
CGFloat height = CGRectGetHeight(drawFrame);
for (CGFloat x = -height; x < CGRectGetWidth(drawFrame); x += 7.5) {
CGPathMoveToPoint(obliquePath, nil, x, 0.0);
CGPathAddLineToPoint(obliquePath, nil, x + height, height);
}
CGContextSetStrokeColorWithColor(context, [UIColor colorWithWhite:0.0 alpha:0.04].CGColor);
CGContextSetLineWidth(context, 2.0);
CGContextAddPath(context, obliquePath);
CGContextStrokePath(context);
CGPathRelease(obliquePath);
} withResult:^(UIView *view) {
[self.view sendSubviewToBack:view];
}];
}
【问题讨论】:
-
在这种情况下,DrawBlock到底是什么?
-
需要 _drawBlock 吗?我没有看到它在任何地方被调用。只有在设置 _drawBlock 时才会调用 setNeedsDisplay。
-
你从哪里给
drawOnView打电话? -
drawInside 是块,不直接调用
-
起来!抱歉,从 viewDidLoad 或 viewWillAppear 调用“drawOnView”。我尝试了多种方法,但没有任何效果
标签: ios objective-c uiview drawrect