0

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

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

    }

}

我怎样才能释放它们,我应该在哪里调用释放方法。

4

2 回答 2

1

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

您拥有所有手势识别器和图像视图的所有权,您应该在不再需要它们时立即释放它们。例如...imageView对象必须在之后释放

[holderView addSubview:imageview];

pinchRecognizer应该在之后立即释放

[holderView addGestureRecognizer:pinchRecognizer];

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

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

于 2012-11-27T20:38:24.270 回答
0

当你遇到内存问题时,如果你有一个名为

- (void)didReceiveMemoryWarning

它会被调用并允许您释放空间。如果您使用的是 ARC,只需将您的 UIImageView 设置为 nil,如果没有,请调用:

[imageview release];
于 2012-11-27T20:14:12.180 回答