0

我遇到了与 UIImageView 相关的内存问题。将此视图添加到我的 UIScrollView 后,如果我尝试释放 UIImageView 应用程序崩溃。根据堆栈跟踪,在调用 [UIImageView dealloc] 之后调用了 [UIImageView stopAnimating]。但是,如果我不释放视图,则内存永远不会释放,并且我已经确认在解除分配后视图上仍有额外的保留调用......这导致我的总分配快速攀升并最终导致应用程序崩溃多次加载视图后。我不确定我在这里做错了什么......我不知道在 UIImageView 发布后试图访问它的是什么。我在下面包含了相关的标头和实现代码(我使用的是 Three20 框架,如果这与它有任何关系......另外,

标题:

@interface PhotoHiResPreviewController : TTViewController <UIScrollViewDelegate> {

    NSString* imageURL;
    UIImage* hiResImage;
    UIImageView* imageView;
    UIView* mainView;
    AppScrollView* mainScrollView;
}

@property (nonatomic, retain) NSString* imageURL;
@property (nonatomic, retain) NSString* imageShortURL;
@property (nonatomic, retain) UIImage* hiResImage;
@property (nonatomic, retain) UIImageView* imageView;

- (id)initWithImageURL:(NSString*)imageTTURL;

执行:

@implementation PhotoHiResPreviewController

@synthesize imageURL, hiResImage, imageView;


- (id)initWithImageURL:(NSString*)imageTTURL {

    if (self = [super init]) {

        hiResImage = nil;

        NSString *documentsDirectory = [NSString stringWithString:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]];
        [self setImageURL:[NSString stringWithFormat:@"%@/%@.jpg", documentsDirectory, imageTTURL]];
    }
    return self;
}

- (void)loadView {

    [super loadView];

    // Initialize the scroll view   
    hiResImage = [UIImage imageWithContentsOfFile:self.imageURL];
    CGSize photoSize = [hiResImage size];
    mainScrollView = [[AppScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    mainScrollView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    mainScrollView.backgroundColor = [UIColor blackColor];
    mainScrollView.contentSize = photoSize;
    mainScrollView.contentMode = UIViewContentModeScaleAspectFit;
    mainScrollView.delegate = self;

    // Create the image view and add it to the scrollview.
    UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, photoSize.width, photoSize.height)];
    tempImageView.contentMode = UIViewContentModeCenter;
    [tempImageView setImage:hiResImage];
    self.imageView = tempImageView;
    [tempImageView release];    
    [mainScrollView addSubview:imageView];

    // Configure zooming.
    CGSize screenSize = [[UIScreen mainScreen] bounds].size;
    CGFloat widthRatio = screenSize.width / photoSize.width;
    CGFloat heightRatio = screenSize.height / photoSize.height;
    CGFloat initialZoom = (widthRatio > heightRatio) ? heightRatio : widthRatio;
    mainScrollView.maximumZoomScale = 3.0;
    mainScrollView.minimumZoomScale = initialZoom;
    mainScrollView.zoomScale = initialZoom;
    mainScrollView.bouncesZoom = YES;

    mainView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    mainView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    mainView.backgroundColor = [UIColor blackColor];
    mainView.contentMode = UIViewContentModeScaleAspectFit;
    [mainView addSubview:mainScrollView];   

    // Add to view  
    self.view = mainView;
    [imageView release];
    [mainScrollView release];   
    [mainView release];
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return imageView;
}

- (void)dealloc {

    mainScrollView.delegate = nil;
    TT_RELEASE_SAFELY(imageURL);
    TT_RELEASE_SAFELY(hiResImage);
    [super dealloc];
}

我不知道如何解决这个问题。如果我在 loadView 方法的末尾删除对 [imageView release] 的调用,一切正常……但我有大量分配,很快就会爬到一个临界点。但是,如果我确实释放它,那么 [UIImageView stopAnimating] 调用会在视图被释放后使应用程序崩溃。

谢谢你的帮助!这几天我一直在用头撞这个。:-P

干杯,约西亚

4

3 回答 3

1

将 imageView 的释放移动到您设置它的点之前。这样,您在更改 imageView 指针指向的位置之前释放对象:

// Create the image view and add it to the scrollview.
UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, photoSize.width, photoSize.height)];
tempImageView.contentMode = UIViewContentModeCenter;
[tempImageView setImage:hiResImage];
// release old object
[imageView release];
// set the pointer to point at a new object
self.imageView = tempImageView;
[tempImageView release];    
[mainScrollView addSubview:imageView];

然后在释放其他对象的方法底部,删除以下行:

[imageView release];
于 2010-03-23T22:26:50.220 回答
0

理想情况下,类级别的变量应该在类的 dealloc() 方法中释放

看起来 imgView 是一个类级别的变量,您在方法调用结束时释放它。

如果您想在方法结束时释放一个变量——它应该是一个方法级变量。因此,在您的情况下,请尝试使 imgView 成为方法级别的变量。然后你应该能够在方法结束时释放它而没有任何问题

于 2010-03-23T22:18:44.867 回答
0

为什么要创建临时视图?你打电话loadView不止一次吗?

self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, photoSize.width, photoSize.height)];
[self.imageView setContentMode:UIViewContentModeCenter];
[self.imageView setImage:hiResImage];
[mainScrollView addSubview:self.imageView];
[self.imageView release]

或者释放它,dealloc因为它是一个实例变量?addSubview保留你的 UIImageView,所以如果它因为对象不存在而崩溃会很奇怪。你试过设置断点吗?

于 2010-03-23T22:20:39.403 回答