Note that there are some explanatory texts on larger screens.

plurals
  1. POUIImageView Allocating Memory Every Time Image Changed - How To Release?
    primarykey
    data
    text
    <p>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!</p> <p>AppDelegate.h</p> <p><pre><code>@interface AppDelegate : NSObject { UIWindow *window; MagazineWebViewController *webViewController; } @property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet MagazineWebViewController *webViewController; - (void)goToA:(NSNumber *)page; @end</pre></code></p> <p>AppDelegate.m</p> <p><pre><code>@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 { [webViewController release]; webViewController = nil; [window release]; [super dealloc]; } @end</pre></code></p> <p>MagazineWebViewController.h</p> <p><code><pre>@interface MagazineWebViewController : UIViewController { UIImageView *imageView; NSNumber *page; } @property (nonatomic, assign)NSNumber *page; @property (nonatomic, retain)UIImageView *imageView; - (void)swipeLeft; - (void)swipeRight; - (void)tableOfContents; @end</pre></code></p> <p>MagazineWebViewController.m</p> <p><code><pre>@implementation MagazineWebViewController @synthesize page, tocController, imageView; - (id)init { UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 768, 60)]; navBar.tintColor = [UIColor blackColor];<br> 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];<br> 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;<br> UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft)]; swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft; [self.view addGestureRecognizer:swipeLeft]; [swipeLeft release];<br> UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRight)]; swipeRight.direction = UISwipeGestureRecognizerDirectionRight; [self.view addGestureRecognizer:swipeRight]; [swipeRight release];<br> [self.view addSubview:self.imageView];<br> [self.imageView release];<br> return self; } - (void)swipeLeft { int pageNum = [page intValue];<br> if (pageNum &lt; 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];<br> if (pageNum > 1) { pageNum--;<br> 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</pre></code></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload