我有一个空白的屏幕,我点击相机按钮并拍照,然后我的空白屏幕上显示的照片为UIVIew,我将多个图像从相机添加到空白屏幕。问题是有时我可以在空白屏幕上添加多个图像,但有时当我捕获图像时,空白屏幕上的所有图像都会消失,只显示当前图像。 在LOG中。它告诉我收到内存警告。 这是我的代码。我想我现在正确发布我的对象。但是当我最后添加[对象发布]时,我的应用程序崩溃了。
-(void)AddImagesToCanvasWithGesture{
if(imageFromPicker.size.width > imageFromPicker.size.height || imageFromPicker.size.width == imageFromPicker.size.height)
{
holderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 180, 160)];
}
else{
holderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 160, 240)];
}
UIImageView *imageview = [[[UIImageView alloc] initWithFrame:[holderView frame]];
[imageview setImage:imageFromPicker];
//[imageview setTag:101];
//[holderView setTag:102];
//NSLog(@"Tag By Default %d",(arc4random()%100)+10);
[holderView setTag:(int)objectDelegate.tagForHolderView];
[imageview setTag:((int)objectDelegate.tagForHolderView)+1];
[holderView addSubview:imageview];
//[imageview canBecomeFirstResponder];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
[pinchRecognizer setDelegate:self];
[holderView addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
[rotationRecognizer setDelegate:self];
[holderView addGestureRecognizer:rotationRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[holderView addGestureRecognizer:panRecognizer];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[holderView addGestureRecognizer:tapRecognizer];
UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)];
[holderView addGestureRecognizer:gestureRecognizer];
[holderView.layer setBorderColor: [[UIColor whiteColor] CGColor]];
[holderView.layer setBorderWidth: 3.0];
[self.view addSubview:holderView];
objectDelegate.tagForHolderView+=2;
for(UIButton *b in self.view.subviews) {
if([b isKindOfClass:[UIButton class]]) {
[self.view bringSubviewToFront:b];
}
[self.view sendSubviewToBack:imageViewForBackground];
//[holderView setHidden:YES];
}
}
我如何发布它们以及我应该在哪里调用release方法。
答案 0 :(得分:1)
从上面添加的代码段中可以看出,您没有正确释放对象。您拥有init
或retain
或copy
的所有对象,除非您使用ARC,否则您有责任释放它们。 (上面的代码适用于ARC)。
您拥有所有手势识别器和图像视图的所有权,您应该在不再需要它们时立即释放它们。例如...... imageView
对象必须在
[holderView addSubview:imageview];
和
pinchRecognizer
应该在
[holderView addGestureRecognizer:pinchRecognizer];
其他手势识别器也是如此。
我猜你的代码的其他部分也有类似的问题,最终应用会收到内存警告。
答案 1 :(得分:0)
当您遇到内存问题时,如果您有一个名为
的方法- (void)didReceiveMemoryWarning
它会被调用并允许你释放空间。如果您使用ARC,只需将UIImageView设置为nil,否则请调用:
[imageview release];