spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Memory warnings when trying to load 200+ images


Your memory management's the problem. Not Swift's. Here's where you went wrong...

In your viewDidLoad method, you've instantiated your imageGroup array with 200+ images. Assuming these images aren't super tiny, you've already allocated a huge chunk of memory to an unnecessary array from the get go. You should instead fetch these images as needed during your loadImage method.

For example, you can replace this line:

var newcontainerView =
UIImageView(image: imageGroup[imageCounter])

with something along the lines of:

let newImage = UIImage(named:
"bg_(imageCounter + 1)")
var newcontainerView = UIImageView(image:
newImage)

so you instead get the image from the bundle on an as-needed basis.

NB: You'll need to figure out an appropriate filename-getting algorithm to suit your case...named: "bg_(imageCounter + 1)" is just an example.

As for your use of imageGroup.count throughout your code, since it's apparent in your viewDidLoad that your images are of a known quantity, I'd suggest replacing all instances of imageGroup.count with an equivalent constant.


Edit:

To address one other issue with your code, calling loadVisibleImages in scrollViewDidScroll can also cause memory issues.

The UIScrollView delegate method scrollViewDidScroll is called continuously as the scrollview scrolls, and thus, as your code stands, loadVisibleImages will be called nearly constantly as well. So especially when the user's scrolling quickly and your app's simultaneously running multiple iterations of loadVisibleImages (where one pass of the method hasn't completed before the next begins), this could result in a crash in this particular case.

So here's a suggestion for now right off the top of my head (I could very well come up with a better one later and I'm sure better ones exist). In your scrollViewDidScroll method, perhaps only call loadVisibleImages if your app isn't already going through it. Then call it again as a safeguard once your scrollview has finished decelerating just to make sure you have the visible images. For example:

var loadingVisibleImages

override func viewDidLoad() {
    super.viewDidLoad()

    loadingVisibleImages = false
    ...
}

func loadVisibleImages() {
    loadingVisibleImages = true
    ...
    loadingVisibleImages = fasle
}

func scrollViewDidScroll(scrollView: UIScrollView)
{
    // Load the pages that are now on screen
    println("did scroll")
    if loadingVisibleImages == false
        loadVisibleImages()
}

func scrollViewDidEndDecelerating(scrollView:
UIScrollView) {
    if loadingVisibleImages == false
        loadVisibleImages()
}

Categories : IOS

Related to : Memory warnings when trying to load 200+ images
Out Of Memory with images in Android 5
Resolved the problem using this guide: http://developer.android.com/training/displaying-bitmaps/index.html

Categories : Java
How to read many images in java without memory leak
My solutions was to use img4Java ( a java interface to ImageMagick commandline). In this way I delegate the image manipulation to an external process freeing JVM to go out fo memory. see http://im4java.sourceforge.net/ and http://www.imagemagick.org/

Categories : Java
Unable to hide PHP warnings
You shouldn't remove warnings system-wide to hide one warning. You should either fix it or hide that particular issue. You can do something like this: function getDB() { return new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE); } $db = @getDB(); The '@' operator ensures that errors or warnings within the function are ignored. Though obviously the best approach would be to fix the

Categories : PHP
Pub warnings while trying to run bwu_datagrid 0.0.18 and polymer+angular
Add a specific version constraint to you pubspec.yaml. dependencies: polymer: '>=0.15.1 <0.16.0' This might lead to follow up errors. Continue accordingly for similar errors. As last resort you can add dependency overrides to force a package no matter if it fulfills the constraints defined in the other imported packages (use at your own risk though). dependency_overrides: some_pac

Categories : Dart
Is it possible to avoid serialization/deserialization and to share big memory object with Memory-mapped files (MMF)?
You can't directly allocate objects in Memory Mapped File with C# (staying in safe code). So it means you need to have some sort of serialization to transfer of data between two applications. Broad options: keep all raw data (more or less as byte array) in the MMF and have C# wrappers to read/write data on demand. Find faster serialization/build one by hand Use some form of change tracking an

Categories : C#
Recently Add
keychain iOS touchId prompted twice
-[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7fc748e37ea0'
Search Results Visibly Scrolling Underneath the Status Bar
How do I exclude an IOS 8 app extension from a build?
EXC_BAD_ACCESS when capturing a GPU frame
iOS 8 - Map keeps reanimating back to user location
Constantly refreshing background image of UIImageView IOS
UINavigationController keeps displaying popped view
Swift closure syntax using Shark Food Mute Switch?
MagicalRecord 3.0 not saving. No objects for the second build
iOS / Swift Can't add gesture to child class
iOS: Is there currently a way to prevent two view controllers being pushed or popped at the same time?
Checking if array contains two objects
How should turning of SCNLight affect background color?
Why don't my views show up in Storyboards and are greyed out in the left pane?
SWIFT convert NSTimeInteval to NSDate
BLE scanning is stopped when go to background?
Drawing a Rect on UIView
HLS playback via local HTTP server on iOS
Re-Assigning SKScene delegate protocol
Network lost on screen lock unlock
Annoying Xcode Error "file not found" #import
CoreData not firing faults
iOS:How do you change a view size/origin from another view controller class?
Changing a frame in viewDidLayoutSubviews
Animate multiple shapes in UIView
Add NSIntegers ONLY when 2nd NSInteger Changes Value
Facebook SDK, fetching friends
How to get Storyboard to support dynamically sized cells in UICollectionView?
GCD / NSOperationQueue EXC_BAD_ACCESS
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.