简体   繁体   中英

Large images in my UIImageView are causing lower views in my UINavigationController stack to release… How do I fix this?

Here is some context to my issue.... using iOS 5 as the target for my iPhone app with ARC on:

I have an app that has a UINavigationController . There are multiple view on the stack that get pushed to the top. The final view that is shown is a UIWebView with Google Images website, and users can select an image and have it sent back to a UIImageView on the view previous in the nav stack. Users can then hit Back and then on the previous view to that hit a Save UIBarButton to save the image locally on their device. Here is what the stack looks like:

MainView -> DetailsView (with Save UIBarButton in upper right) -> ImageDisplayView -> WebView (with Google Images website loaded)

The problem is, when a user selects a large image from Google Images (lets say a 5MB image) using the View Full Image button on Google Images Mobile, a memory warning is thrown and the views below it are released. So when a user goes back to the DetailsView with the Save button, the Save button is now gone. This is because didReceiveMemoryWarning is called.

So my questions are....

  1. Why is a memory warning called when displaying a 5MB image in an embedded UIWebView?

  2. How do I prevent the memory warning from causing issues (like dropping UIBarButton views) lower in my navigation stack?

  3. Are there any tips/tricks that anyone knows to get full sized images properly from Google Images mobile within an iPhone app?

To try this problem for yourself, create a UIWebView in a blank app and just load Google Images website into it. Install it on your device (3GS or lower will make the problem appear quicker) and just load full images into and out of the browser... you'll eventually get the warning in your debug console.

Check out the iOS documentation for "Responding to Low-Memory Warnings in iOS" where it states "View controllers automatically purge any views that are currently offscreen" Also look at "The View Controller Life Cycle". Your view controllers will get viewWillUnload: and viewDidUnload: messages. You'll have to recreate the views using, for example loadView (if not using Storyboards), when they are next needed.

Specifically answering your questions:

  1. A memory warning is generated when memory is needed but not available. Even needing one byte can trigger the warning - it depends on how much is needed.
  2. The views will be dropped; it is your responsibility to recreate them. How you recreate them depends on how they were created initially (programmatically, by nib files, by storyboards).
  3. Seems you are getting images just fine but need to deal with the low memory warnings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM