Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

grandM

macrumors 68000
Original poster
Oct 14, 2013
1,568
329
If you want to put multiple images (amount unknown) in a scrollview is a collectionView the way to go?
 
Few things:
Scrolling direction
Horizontal - Collection
Vertical - Table

Columns?
Yes - Collection View
No - Table View

Obviously there are further arguments for/against each
 
  • Like
Reactions: grandM
Displaying multiple images is something a collection view was designed to do. Most users are already familiar with it. I suggest putting all the images in a collection view, and then when the user taps on an image, make that one image full screen in a scroll view so that the user can zoom in and out.
 
  • Like
Reactions: grandM
Displaying multiple images is something a collection view was designed to do. Most users are already familiar with it. I suggest putting all the images in a collection view, and then when the user taps on an image, make that one image full screen in a scroll view so that the user can zoom in and out.
Jep I implemented the collection View and managed to fill it with the photos in Core Data. I still have to create thumbnails as you suggested. Was the idea though. I'm still struggling with something however. I was trying to make the photos oval instead of rectangular. For some reason this isn't working. The code is:
Code:
import UIKit


class GalleryItemCollectionViewCell: UICollectionViewCell {

 

    @IBOutletweakvar itemImageView: UIImageView!

 

    func setCellWithGalleryItem(item: Photo) {

        // Setting a photo is done in a queue

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), { () -> Voidin

            if let photoData = item.photo {

                let photoImage = UIImage(data: photoData)

             

                dispatch_async(dispatch_get_main_queue(), { () -> Voidin

                    self.itemImageView.layer.cornerRadius = 30.0

                    self.itemImageView.clipsToBounds = true

                    self.itemImageView.image = photoImage

                    self.itemImageView.layer.cornerRadius = 30.0

                    self.itemImageView.clipsToBounds = true

                })

            }

        })

    }


}

I was also wondering how custom views are made? I saw it being done programmatically but can you also use a View in IB? I mean you could add a view of course in IB and lay out the elements. But could you also make a view dragging and dropping and save it as an UIView? Or must this be done in code?
 
Why save an image to core data in the first place? Core data is not a good place to store image data. Instead I would suggest saving the images as jpegs or pngs in the device file system and simply maintaining a reference to the file using its filename (i.e. the image belonging to userX is userX0.jpg).

About creating the oval display effect, what is happening instead? They're just showing up as regular rectangle image views as if the cornerRadius property is having no effect?
 
Why save an image to core data in the first place? Core data is not a good place to store image data. Instead I would suggest saving the images as jpegs or pngs in the device file system and simply maintaining a reference to the file using its filename (i.e. the image belonging to userX is userX0.jpg).

About creating the oval display effect, what is happening instead? They're just showing up as regular rectangle image views as if the cornerRadius property is having no effect?
Core Data has this option use external storage. This would store the images in a folder, not in the DB. Still have to find out how it factually operates. How do you create a unique id for your pictures then: a class count? Yes, it stays rectangular.
 
Core Data has this option use external storage. This would store the images in a folder, not in the DB. Still have to find out how it factually operates. How do you create a unique id for your pictures then: a class count? Yes, it stays rectangular.
The best way I've found is to generate a completely random long string of characters then append the string with UNIX time in milliseconds. The chance of another file name out there ever matching is less than 1 in 100 trillion.

Also, when and where is this code getting called from? What is the originating method that starts it all? If you try to set properties like cornerRadius in viewDidLoad or any other method that happens too early in the view controller lifecycle you will often encounter strange results. You should instead ensure that the cornerRadius is set in view controllers' viewDidLayoutSubviews method so that it only gets called after auto layout has done its magic and the su views have a real actual frame.
 
The best way I've found is to generate a completely random long string of characters then append the string with UNIX time in milliseconds. The chance of another file name out there ever matching is less than 1 in 100 trillion.

Also, when and where is this code getting called from? What is the originating method that starts it all? If you try to set properties like cornerRadius in viewDidLoad or any other method that happens too early in the view controller lifecycle you will often encounter strange results. You should instead ensure that the cornerRadius is set in view controllers' viewDidLayoutSubviews method so that it only gets called after auto layout has done its magic and the su views have a real actual frame.
it's done in the collection View datasource. I moved the func to viewDidLayoutSubViews but without any result.
 
I think AN's suggestion hit the nail on the head.

Core Data's external storage was temperamental the last time I had the opportunity to use it (that was several generations ago, but am still weary), so I would just avoid usage of it - it's important to take into consideration the type of data and how they're externally stored, too (<100kb is stored in DB, <1mb is stored in DB on another table, 1+mb is only what is stored externally).
 
it's done in the collection View datasource. I moved the func to viewDidLayoutSubViews but without any result.
I've dealt with this problem before. You will want to create a custom .xib file for the view inside the collection view, and subclass it. Then in the didMoveToSuperview method, set the cornerRadius there. That will work.
 
I think AN's suggestion hit the nail on the head.

Core Data's external storage was temperamental the last time I had the opportunity to use it (that was several generations ago, but am still weary), so I would just avoid usage of it - it's important to take into consideration the type of data and how they're externally stored, too (<100kb is stored in DB, <1mb is stored in DB on another table, 1+mb is only what is stored externally).
Hmm in that case I will use a static count to increase an ID for the photos. Then store the photos in a folder. If this external storage is still being stored in the database it could ruin performance?
 
I've dealt with this problem before. You will want to create a custom .xib file for the view inside the collection view, and subclass it. Then in the didMoveToSuperview method, set the cornerRadius there. That will work.
Do you have an example by any chance? I did not build my own views yet.
 
Hmm in that case I will use a static count to increase an ID for the photos. Then store the photos in a folder. If this external storage is still being stored in the database it could ruin performance?

Well, the underlying database is SQLite, which is very good, but also not being used as-is by Core Data.

The real issue is that you're doing a lot of this work on an iPhone, iPad, or Apple Watch (!!!). The latter being the most important reason to keep your model as slim as possible (especially if you share a framework). As you start to accumulate large amounts of data (say many <1mb blobz), more effort will need to be exerted by the device to retrieve any information.

It's important to keep your indices in order - always, but even more if you expect to be storing thick rows or many rows.

Depending on your scope, I'd say look into it, though. As I type this I think about how much more powerful the devices have become in recent years.
 
  • Like
Reactions: grandM
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.