gpt4 book ai didi

iphone - IOS 中的内存管理问题(收到内存警告。)

转载 作者:行者123 更新时间:2023-11-29 04:16:45 25 4
gpt4 key购买 nike

我有一个空白屏幕,我在其中单击相机按钮并拍照,然后该照片作为 UIVIew 显示在我的空白屏幕上,并且我将相机中的多个图像添加到空白屏幕。问题是有时我可以在空白屏幕上添加多个图像,但有时当我捕获图像时,空白屏幕上的所有图像都会消失,只显示当前图像。并在日志中。它显示我收到内存警告。这是我的代码。我想我现在正在正确地释放我的对象。但是当我最后添加 [对象释放] 时,我的应用程序崩溃了。

   -(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];

}

}

我如何释放它们以及我应该在哪里调用释放方法。

最佳答案

从上面添加的代码片段中,很明显您没有正确释放对象。您拥有 initretaincopy 的每个对象,并且您有责任释放它们,除非您使用 ARC。 (上面的代码适用于 ARC)。

您拥有所有手势识别器和 ImageView 的所有权,一旦不再需要它们,您应该立即释放它们。例如... imageView 对象必须在

之后释放

[holderView addSubview:imageview];

pinchRecognizer 应在之后立即释放

[holderView addGestureRecognizer:pinchRecognizer];

其他手势识别器也是如此。

我猜您代码的其他部分也有类似的问题,最终应用程序收到内存警告。

关于iphone - IOS 中的内存管理问题(收到内存警告。),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13592114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com