我有一个iPad应用程序在iPad(第一个型号)上崩溃,因为内存不足。
我创建一个名为RoadSectionView的自定义视图,这意味着一条道路,另一个名为CrossRoadView的自定义视图包含四个RoadSectionViews。
RoadSectionView.h:
#import "GestureView.h"
@interface RoadSectionView : GestureView <NSCopying,NSCoding>
@property (nonatomic, assign) CGPoint startPoint; //手指触摸起始点
@property (nonatomic, assign) CGPoint endPoint; //手指触摸终止点
@property (nonatomic, strong) UIImageView *startPointView; //起始控制点
@property (nonatomic, strong) UIImageView *endPointView; //终止控制点
@property (nonatomic)NSString *type; //路段的类型
@property (nonatomic)NSString *roadName; //路段名称
@property (nonatomic, assign)NSInteger roadWidth; //路道宽,默认为350
@property (nonatomic, assign)NSInteger roadIslandWidth; //中间路岛宽,默认为40
@property (nonatomic, assign)NSInteger upOrLeftLaneNum; //上或左路段的路道数,默认为2
@property (nonatomic, assign)NSInteger downOrRightLaneNum; //下或右路段的路道数,默认为2
@property (nonatomic) NSArray *startUpOrLeft; //上或左起始点数组
@property (nonatomic) NSArray *startDownOrRight; //下或右起始点数组
@property (nonatomic) NSArray *endUpOrLeft; //上或左终止点数组
@property (nonatomic) NSArray *endDownOrRight; //下或右终止点数组
- (void)drawView:(CGPoint)point1 :(CGPoint)point2;
- (void)addControlPoint:(CGPoint) start : (CGPoint) end;
- (id)initWithFrame:(CGRect)frame : (NSString *)name;
@end
这是CrossRoadView.h
#import "GestureView.h"
#import "RoadSectionView.h"
@interface CrossRoadView : GestureView<NSCopying,NSCoding>
@property (nonatomic) NSString *type;
@property (nonatomic, assign) CGPoint centerPoint;
@property (nonatomic, strong)RoadSectionView *leftRoad;
@property (nonatomic, strong)RoadSectionView *rightRoad;
@property (nonatomic, strong)RoadSectionView *upRoad;
@property (nonatomic, strong)RoadSectionView *downRoad;
- (void)drawView:(CGPoint)start;
- (id)initWithFrame:(CGRect)frame : (NSString *)name;
- (void)addControlPoint;
- (void)addDistanceLine;
- (void)drawConnectLines;
@end
GestureView.h文件:
#import <UIKit/UIKit.h>
@interface GestureView : UIImageView <UIGestureRecognizerDelegate, UIPopoverControllerDelegate,NSCoding,NSCopying>
@property (nonatomic, retain) UIView *parentView;
@property BOOL isPanEnable;
@property BOOL isRotateEnable;
@property BOOL isPinchEnable;
@property BOOL doesHaveTreelawn;
@property (nonatomic) CGPoint imagePoint; //记录添加的view的在父视图中的起点
@property (nonatomic) CGFloat imageSize; //记录放缩的倍数
@property (nonatomic) CGFloat imageRotation; //记录旋转的角度
@property (nonatomic) CGPoint imageTranslate; //记录平移的大小
@property (nonatomic, assign) CGPoint upContrlPoint;
@property (nonatomic, assign) CGPoint downContrlPoint;
@property (nonatomic, retain) UIPopoverController *popoverController;
- (void)handlePan:(UIPanGestureRecognizer *)recognizer;
- (void)handlePinch:(UIPinchGestureRecognizer *)recognizer;
- (void)handleRotate:(UIRotationGestureRecognizer *)recognizer;
- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer;
- (void)setGesture:(UIView *)parent : (BOOL)isPan : (BOOL)isPinch : (BOOL)isRotate;
@end
当我在rootView中添加几个RoadSectionView,而不是删除它们时,它工作正常,真正的内存没有增加。 删除代码是
[self.canvasView.subviews makeObjectsPerformSelector: @selector(removeFromSuperview)];
但是当我在rootView中添加几个CrossRoadView时,我删除了它们,实际内存增加,因为我将它们保存到磁盘,canvasView将在打开文件时首先删除所有子视图,而不是我多次打开文件,应用程序崩溃了
你可以帮我!!非常感谢答案 0 :(得分:0)
您可能不需要为strong
使用RoadSectionView
属性,因为当您将它们添加为子视图时,超级视图(CrossRoadView
)将保留这些属性。
然后,当超级视图被摧毁时,它们将被释放。
尝试使用
@property (nonatomic, weak) RoadSectionView *leftRoad
...