每次图像更改时UIImageView分配内存 - 如何发布?

时间:2022-09-02 11:12:22

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上面有一个UIView类,向左/向右滑动我要么向前还是向后移动页面。我的问题是,当我滑动并更改图像源时,程序会继续分配更多内存,而我不确定在何处/如何释放先前分配的内存。我已经了解了imageNamed和imageWithContentsOfFile之间的区别,我相信我正确地使用了这些,所以我很难过。任何帮助将非常感激!

AppDelegate.h

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

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 {
    [webViewController release];
    webViewController = nil;
    [window release];
    [super dealloc];
}
@end

MagazineWebViewController.h

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

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

2 个解决方案

#1


0  

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

在AppDelegate.m中,尝试使用'self'为webViewController的每个引用添加前缀。例如:

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

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!

如果不使用self引用该属性,则会绕过属性自动生成的setter和getter。并且由于您将此属性设置为retain,这将绕过一些内存处理逻辑。我很确定这是你的问题...希望这有帮助!

#2


0  

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

我认为这是因为你每次都会分配一个UIImage(就像评论中所说的那样)。

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.

但是很难说没有你所有程序的泄漏来自哪里^^也许是保留财产或类似的东西。

#1


0  

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

在AppDelegate.m中,尝试使用'self'为webViewController的每个引用添加前缀。例如:

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

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!

如果不使用self引用该属性,则会绕过属性自动生成的setter和getter。并且由于您将此属性设置为retain,这将绕过一些内存处理逻辑。我很确定这是你的问题...希望这有帮助!

#2


0  

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

我认为这是因为你每次都会分配一个UIImage(就像评论中所说的那样)。

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.

但是很难说没有你所有程序的泄漏来自哪里^^也许是保留财产或类似的东西。