multiple images in a scrollview

Discussion in 'iOS Programming' started by grandM, Dec 20, 2015.

  1. grandM macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #1
    If you want to put multiple images (amount unknown) in a scrollview is a collectionView the way to go?
     
  2. JWalker1995 macrumors regular

    Joined:
    Mar 5, 2011
    #2
    Few things:
    Scrolling direction
    Horizontal - Collection
    Vertical - Table

    Columns?
    Yes - Collection View
    No - Table View

    Obviously there are further arguments for/against each
     
  3. AxoNeuron macrumors 65816

    AxoNeuron

    Joined:
    Apr 22, 2012
    Location:
    The Left Coast
    #3
    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.
     
  4. grandM thread starter macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #4
    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?
     
  5. AxoNeuron macrumors 65816

    AxoNeuron

    Joined:
    Apr 22, 2012
    Location:
    The Left Coast
    #5
    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?
     
  6. grandM thread starter macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #6
    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.
     
  7. AxoNeuron macrumors 65816

    AxoNeuron

    Joined:
    Apr 22, 2012
    Location:
    The Left Coast
    #7
    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.
     
  8. grandM thread starter macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #8
    it's done in the collection View datasource. I moved the func to viewDidLayoutSubViews but without any result.
     
  9. Mascots macrumors 65816

    Mascots

    Joined:
    Sep 5, 2009
    #9
    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).
     
  10. AxoNeuron macrumors 65816

    AxoNeuron

    Joined:
    Apr 22, 2012
    Location:
    The Left Coast
    #10
    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.
     
  11. grandM thread starter macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #11
    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?
     
  12. grandM thread starter macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #12
    Do you have an example by any chance? I did not build my own views yet.
     
  13. Mascots macrumors 65816

    Mascots

    Joined:
    Sep 5, 2009
    #13
    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.
     
  14. AxoNeuron macrumors 65816

    AxoNeuron

    Joined:
    Apr 22, 2012
    Location:
    The Left Coast
    #14
     

Share This Page