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または、インスタンス変数なので解放しますか? addSubviewUIImageView を保持するため、オブジェクトが存在しないためにクラッシュした場合は奇妙になります。ブレークポイントを設定しようとしましたか?

于 2010-03-23T22:20:39.403 に答える