我为UIView
创建子类以便在视图上绘制自定义数字,视图本身会非常大,最高可达10000 x 10000,我们正在使用这个大视图,因此我们可以容纳高级别变焦。我已尝试通过覆盖drawRect:
或添加子图层来绘制视图上的各种内容。然而,在缩放或滚动运动的子图层变得生涩并且具有可见的滞后。这是因为子层被重绘了很多。
使用drawRect:
然而事情非常顺利,除非我覆盖drawRect:
有大量的内存使用量,对于2000 x 2000大小的视图,它需要70.5 mb(我正在调用[super drawRect:rect]
没有好处)。如果我不覆盖drawrect我可以将我的自定义视图设置为我尝试过的任何尺寸甚至30000 x 30000,在内存方面没有问题。但是,只要drawRect:
覆盖内存泄漏就会发生。我的项目是基于ARC的,当我使用drawRect:
绘制自定义形状时,我对性能非常满意,所以我想知道是否有办法插入内存泄漏?或优化一下。
非常感谢任何帮助,如果我没有澄清某事,请告诉我
此致
main / default viewcontroller.h
#import <UIKit/UIKit.h>
#import "P10Maps.h"
@interface P10ViewController : UIViewController {
IBOutlet UIScrollView *scrollView;
}
@end
主要/默认viewcontroller.m
#import "P10ViewController.h"
@interface P10ViewController ()
@end
@implementation P10ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//[self initializeViews];
//[self coordinateGen];
mapView = [[P10Maps alloc] initWithFrame:CGRectMake(0, 0, 2000, 2000)];
scrollView.contentSize = CGSizeMake(2000,2000);
scrollView.minimumZoomScale = -10;
scrollView.maximumZoomScale = 10;
[scrollView setZoomScale:0.2 animated:YES];
scrollView.delegate = (id)self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return mapView;
}
@end
分别是UIview .h和.m的子类
#import <UIKit/UIKit.h>
#import "P10ViewController.h"
@interface P10Maps : UIView
@end
#import "P10Maps.h"
@implementation P10Maps
int countryID;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
NSLog(@"frame %@", NSStringFromCGRect(frame));
[self drawPaths];
if (self) {
//self.opaque = YES;
self.backgroundColor = [UIColor whiteColor];
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
}
@end