Hamza Hassan Hamza Hassan - 10 months ago 32
Swift Question

i have an issue on the loading images on my custom collectionView

i have tableViews which contains each row folder(Album Image) which i save images to it in the document directory
when i select the each folder i enter in the custom collectionView which contains all the images in the document directory and display it on the cells my issue is collectionView very slow loading the images in the collectionView Cells it take 5 to 7 seconds when loading all the images in the collectionView Cells

the following below my code in viewWillAppear:

override func viewWillAppear(animated: Bool) {

setEditing(false, animated: true)
arrayOfImagesFromDocumentDirectory = []
dictionaryOfGetImageUrl = [:]
dictionaryOfImagesForExport = [:]
arrayImageForMove = []
moveArrayImageOfUrl = []
arrayOfImageUrl2 = []

self.navigationItem.title = albumName

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
dispatch_async(dispatch_get_main_queue(), {

let fileManager2 = NSFileManager.defaultManager()
do {
let document = try fileManager2.URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: false)
let getFolders = try fileManager2.contentsOfDirectoryAtURL(document, includingPropertiesForKeys: nil, options: .SkipsHiddenFiles)

for folder in getFolders {
if folder.lastPathComponent! == albumName {
let getImages = try fileManager2.contentsOfDirectoryAtURL(folder, includingPropertiesForKeys: nil, options: .SkipsHiddenFiles)

for img in getImages {
self.arrayOfImagesFromDocumentDirectory.append(UIImage(contentsOfFile: img.path!)!)


}catch {

self.arrayOfImg = []

for img in self.arrayOfImagesFromDocumentDirectory {
let img2:UIImage = self.ResizeImage(img, targetSize: CGSize(width: 140, height: 140))




the following Below MyCode on the cellForItemAtIndexPath:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! AlbumImagesCollectionViewCell

if self.dictionaryOfSelectItem.count > 0 {
if self.dictionaryOfSelectItem[indexPath.row] == indexPath.row {

cell.imageViewCheck.image = UIImage(named: "check.png")
}else {
cell.imageViewCheck.image = nil

}else {
cell.imageViewCheck.image = nil

if self.arrayOfImageUrl2.count > 0 && self.arrayOfImagesFromDocumentDirectory.count > 0 {
if self.arrayOfImg.count > 0 {
cell.imageViewCell.image = self.arrayOfImg[indexPath.row]
cell.imageUrlLabel.text = self.arrayOfImageUrl2[indexPath.row]


return cell


any idea for solve my big problem

Thanks Advance


I have just seen that you are running a background thread but inmediatelly going back the main thread. You need to load the data (images in this case) in the background thread

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {

Also, if you load the images in the viewDidLoad instead of in the viewWillAppear then you don't need to reload the collection view, because it will load after that and use the data source (in your case self.arrayOfImg) to populate.

I also suggest you not to resize the images in the code but use constrains to do that.

Is it possible that your images are too big?