Memory continuously increase then app crash when i display image from document directory during scrolling UITableview

后端 未结 3 1534
臣服心动
臣服心动 2021-01-19 15:18

My Requirement is download all images in application memory and display it from local if its available.

Below is my code to access image from local and if its not av

相关标签:
3条回答
  • 2021-01-19 16:04

    It's possible that UIImagePNGRepresentation returns non-autoreleased object - you can try to release it and see if that results in a crash. Obviously you are not releasing something, but nothing other than the image representation appears obvious.

    A few other comments:

    • run your app in Instruments, using the ObjectAlloc tool, and it should be immediately obvious what objects are not dealloced. If you don't know Instruments, well, its time now to learn it.

    • you can 'track' objects and get a message when they are dealloced using ObjectTracker - however it was designed for ARC so you may need to tweak it. If you use it you would see a message when each of your objects are dealloced

    • when the table view is done with a cell, there is a delegate method that you can receive that tells you so, and you can then nil (release) and objects the cell retains

    • your use of downloadQueue is bizarre - create it once in your instance as an ivar, use it as you need, and in dealloc release it

    • you hide the activity spinner on the main queue, but don't start it on the main queue

    • you command the activity view to remove itself from its superview, but then look for in in the subviews and try to remove it there:

    [self.activityView removeFromSuperview];

    for (UIView * view in self.subviews)
    {
        if([view isKindOfClass:[UIActivityIndicatorView class]])
            [view removeFromSuperview];
    }
    

    In the end, Instruments is what you want. You can read up more about it here, or just google and you will surely find a slew of blogs to read.

    0 讨论(0)
  • 2021-01-19 16:08

    As per my Understanding there is an issue in your "getSavedImage" Method you have to manage memory Manually instead of 'autorelease' so as My suggestion is use

    UIImage *image = [[UIImage alloc] initWithContentsOfFile:filePath]
    and also release it after use of it. means after '[self setImage:saveImg];'
    [saveImg release]
    

    instead of this.

    [[UIImage imageWithContentsOfFile:filePath] retain];
    

    'Don't Use Autorelease because it has staying in memory until pool not drain' and just because of this you got an memory issue.

    0 讨论(0)
  • 2021-01-19 16:11

    Yes Finally i have resolved it.

    The code which is in Question is working fine now. but Without release some objects and @autoreleasepool block which is in code, memory was increase continuously during scroll UITableView.

    From the Instrument i found that memory increase in UILableView and UIImageView. I am using Custom UITableViewCell and in that file i havnt implement dealloc method. So When i have implement dealloc method in UITableViewCell .m file and release & nil all object.

    After that memory not increase during scroll TableView and its Resolved the issue.

    0 讨论(0)
提交回复
热议问题