0

I'm attempting to retrieve images from the user's camera roll but I'm encountering an issue where it runs out of memory. I load 50 images at a time, but unfortunately after a few loads it runs out of memory and crashes. I've tried wrapping the entire statement in an autoreleasepool but that proved not effective. here is my code...

func retrieveImages(thumbnail: Bool, indexOfLibrary: Int) {
    /* Retrieve the items in order of modification date, ascending */
    var indexSet: NSIndexSet!

    let options = PHFetchOptions()

    options.sortDescriptors = [NSSortDescriptor(key: "creationDate",
        ascending: false)]

    /* Then get an object of type PHFetchResult that will contain
    all our image assets */
    let assetResults = PHAsset.fetchAssetsWithMediaType(.Image,
        options: options)

    if totalPostsCounted == false {
        assetResults.enumerateObjectsUsingBlock { (object: AnyObject!, count: Int, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
            if object is PHAsset {
                self.totalPosts = count
            } else {
                self.totalPostsCounted = true
            }
        }
    }

    if thumbnail == true {
        if totalPosts - libraryImageThumbnails.count < 50 {
            allImagesLoaded = true
        } else {
            if firstTimeLoadedThumb == true {

            } else {
                firstTimeLoadedThumb = true

            }
        }

    } else {
        if totalPosts - libraryImages.count < 50 {
            allImagesLoaded = true
        } else {
            if firstTimeLoaded == true {
            } else {
                firstTimeLoaded = true

            }
        }


    }


    if thumbnail == true {
        fiftyIncrementThumb = fiftyIncrementThumb + 50
        increment = fiftyIncrementThumb
    } else {
        fiftyIncrement = fiftyIncrement + 50
        increment = fiftyIncrement
    }

    let imageManager = PHCachingImageManager()
    autoreleasepool { () -> () in

        assetResults.enumerateObjectsUsingBlock { (object, count: Int, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
            if object is PHAsset {

                var imageSize: CGSize!

                var array: NSArray!

                if thumbnail == true {
                    array = self.libraryImageThumbnails
                } else {
                    array = self.libraryImages
                }
                autoreleasepool {
                    if count >= array.count && count <= increment {
                        if thumbnail == true {
                            imageSize = CGSize(width: 100, height: 100)

                        } else {
                            imageSize = CGSize(width: self.cameraView.bounds.width, height: self.cameraView.bounds.height)

                        }

                        let asset = object as! PHAsset
                        let options = PHImageRequestOptions()
                        options.deliveryMode = .FastFormat
                        options.synchronous = true

                        imageManager.requestImageForAsset(asset,
                            targetSize: imageSize,
                            contentMode: .AspectFill,
                            options: options,
                            resultHandler: { (image, _: [NSObject : AnyObject]?) -> Void in
                                if thumbnail == true {
                                    self.libraryImageThumbnails.append(image!)
                                } else {
                                    self.libraryImages.append(image!)
                                }
                        })
                    }
                }
            }
        }


    }


}

I also tried this and it returned nil and crashed...

    let library = ALAssetsLibrary()
        library.enumerateGroupsWithTypes(ALAssetsGroupAll, usingBlock: { (group: ALAssetsGroup!, stop: UnsafeMutablePointer<ObjCBool>) -> Void in

            group.enumerateAssetsUsingBlock({ (asset, count: Int, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
                if asset != nil {
                    self.libraryImageThumbnails.append(asset)
                    self.collectionTable.reloadData()

                }

            })
            }, failureBlock: { (error: NSError!) -> Void in

        })

Tell me if you need any more information! Thank you!

John Doe
  • 1,609
  • 2
  • 13
  • 22

3 Answers3

3

Here's something I've done in a project that might help:

Some properties:

let requestOptions = PHImageRequestOptions()
let fetchOptions = PHFetchOptions()
let cachingImageManager = PHCachingImageManager()
var assets: [PHAsset] = [] {
    willSet {
        cachingImageManager.stopCachingImagesForAllAssets()
    }

    didSet {
        cachingImageManager.startCachingImagesForAssets(assets,
            targetSize: PHImageManagerMaximumSize,
            contentMode: .AspectFit,
            options: self.requestOptions
        )
    }
}


func fetchPhotos() {
    requestOptions.resizeMode = PHImageRequestOptionsResizeMode.Exact
    requestOptions.version = .Current
    requestOptions.deliveryMode = PHImageRequestOptionsDeliveryMode.HighQualityFormat
    requestOptions.synchronous = true

    fetchOptions.sortDescriptors = [NSSortDescriptor(key:"creationDate", ascending: false)]
    if #available(iOS 9.0, *) {
        fetchOptions.fetchLimit = 50
    } else {

        // Fallback on earlier versions
    }

    fetchResults = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image, options: fetchOptions)
    guard let fetchResults = fetchResults else {
        print("No PHAssets")
        return
    }
    dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)) { [weak self] in
        fetchResults.enumerateObjectsUsingBlock{ object, index, stop in
            let asset = object as! PHAsset
            self?.assets.append(asset)
        }
        dispatch_async(dispatch_get_main_queue()) {
            self?.photoCollectionView.reloadData()
        }
    }
}
jasonnoahchoi
  • 871
  • 5
  • 16
1

Try this code and make sure you import <AssetsLibrary/AssetsLibrary.h> and make you deployment target as 7.1.

objective-C code

NSMutableArray *imageArray=[[NSMutableArray alloc] init];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{

    [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
     {
         if (asset) {
             [imageArray addObject:asset];
             NSLog(@"imageArray :: %@",imageArray);
         }
     }];
} failureBlock:^(NSError *error) {
    NSLog(@"error :: %@",error);
}];

Swift code

var imageArray: [AnyObject] = [AnyObject]()
var library: ALAssetsLibrary = ALAssetsLibrary()

   library.enumerateGroupsWithTypes(ALAssetsGroupSavedPhotos, usingBlock: {(group: ALAssetsGroup, stop: Bool) -> Void in
    group.enumerateAssetsUsingBlock({(asset: ALAsset, index: Int, stop: Bool) -> Void in
        if asset != nil {
            imageArray.append(asset)
            NSLog("imageArray :: %@", imageArray)
        }
   })
}, failureBlock: {(error: NSError) -> Void in
NSLog("error :: %@", error!)
})

result: enter image description here

check for below url for reference

https://stackoverflow.com/a/15570221/4101371

https://stackoverflow.com/a/25732673/4101371

https://stackoverflow.com/a/32472476/4101371

and you can also use photos framework from iOS 8 onwards. photos Framework

Hope it will help.

Community
  • 1
  • 1
Dhaivat Vyas
  • 2,926
  • 1
  • 16
  • 26
  • I just tried one of the suggestions and I edited my question to show what happened when I did. – John Doe Jan 26 '16 at 06:29
  • Mmm for some reason when I try that it works, but when I try and set the image in the cellForRow it crashes. – John Doe Jan 26 '16 at 07:08
1

Setting the code below worked for me to limit the fetch

let assetFetchOptions = PHFetchOptions()
assetFetchOptions.fetchLimit = 50

Then I only retrieved 50 photos as oppose to 15,000 on my friends phone!!

nlapham21
  • 79
  • 5