开发者

UIImageView Allocating Memory Every Time Image Changed - How To Release?

I'm trying to create an application that allows users to go through a set of images like a magazine. I've got one UIView class on top of the AppDelegate and on swipe to the left/right I'm either going forward or backward a page. My problem is that when I swipe and change the image source the program keeps allocating more memory, and I'm not certain where/how to release the previously allocated memory. I've looked in to the difference between imageNamed and imageWithContentsOfFile and I believe I'm using those correctly so I'm stumped. Any help would be much appreciated!

AppDelegate.h

@interface AppDelegate : NSObject  {
    UIWindow *window;
    MagazineWebViewController *webViewController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet MagazineWebViewController *webViewController;
- (void)goToA:(NSNumber *)page;
@end

AppDelegate.m

@implementation AppDelegate
@synthesize window, webViewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
    webViewController = [[MagazineWebViewController alloc] init];
    NSNumber *page = [NSNumber numberWithInt:1];
    [webViewController setPage:page];
    [window addSubview:webViewController.view];
    [window makeKeyAndVisible];
}
- (void)goToA:(NSNumber *)page {
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@", page] ofType:@"png"]];
    webViewController.imageView.image = image;
    [image release];
    [webViewController setPage:page];
}
- (void)dealloc {
    [webViewCon开发者_运维问答troller release];
    webViewController = nil;
    [window release];
    [super dealloc];
}
@end

MagazineWebViewController.h

@interface MagazineWebViewController : UIViewController {
    UIImageView *imageView;
    NSNumber *page;
}
@property (nonatomic, assign)NSNumber *page;
@property (nonatomic, retain)UIImageView *imageView;
- (void)swipeLeft;
- (void)swipeRight;
- (void)tableOfContents;
@end

MagazineWebViewController.m

@implementation MagazineWebViewController
@synthesize page, tocController, imageView;
- (id)init {
    UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 768, 60)];
    navBar.tintColor = [UIColor blackColor];

UIBarButtonItem *contents = [[UIBarButtonItem alloc] initWithTitle:@"Table of Contents" style:UIBarButtonItemStylePlain target:self action:@selector(tableOfContents)]; UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"Title"]; item.leftBarButtonItem = contents; item.hidesBackButton = YES; [navBar pushNavigationItem:item animated:NO]; [contents release]; [item release]; [self.view addSubview:navBar]; [navBar release];

self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 40, 768, 984)]; UIImage *image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"1" ofType:@"png"]]; self.imageView.image = image; [image release]; self.imageView.contentMode = UIViewContentModeScaleToFill;

UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft)]; swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft; [self.view addGestureRecognizer:swipeLeft]; [swipeLeft release];

UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRight)]; swipeRight.direction = UISwipeGestureRecognizerDirectionRight; [self.view addGestureRecognizer:swipeRight]; [swipeRight release];

[self.view addSubview:self.imageView];

[self.imageView release];

return self; } - (void)swipeLeft { int pageNum = [page intValue];

if (pageNum < 115) { pageNum++; UIViewAnimationTransition trans = UIViewAnimationTransitionCurlUp; [UIView beginAnimations:nil context:nil]; [UIView setAnimationTransition:trans forView:self.view.window cache:YES]; BaseballMagazine2011AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; [appDelegate goToA:[NSNumber numberWithInt:pageNum]]; [UIView commitAnimations]; } } - (void)swipeRight { int pageNum = [page intValue];

if (pageNum > 1) { pageNum--;

UIViewAnimationTransition trans = UIViewAnimationTransitionCurlDown; [UIView beginAnimations:nil context:nil]; [UIView setAnimationTransition:trans forView:self.view.window cache:YES]; BaseballMagazine2011AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; [appDelegate goToA:[NSNumber numberWithInt:pageNum]]; [UIView commitAnimations]; } } - (void)tableOfContents { } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc. that aren't in use. } - (void)viewDidUnload { [imageView release]; imageView = nil; [page release]; page = nil; [tocController release]; tocController = nil; [super viewDidUnload]; } - (void)dealloc { [imageView release]; imageView = nil; [page release]; page = nil; [super dealloc]; } @end


In your AppDelegate.m, Try prefacing each reference to webViewController with 'self.'. For example:

self.webViewController = [[MagazineWebViewController alloc] init];

By not referencing the property with self, you are bypassing the properties auto generated setters and getters. And since you set this property to retain, this would bypasses some memory handling logic. I'm pretty sure that is your issue... Hope this helps!


I think it's because you alloc every time an UIImage (like said in the comments).

Did you try not doing allocations ?

self.imageView.image = [ UIImage imageNamed:@"1.png" ]; 

I think you won't have any problem with this.

But it's difficult to say where come from the leak without all your program ^^
Maybe a retain property or something like that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜