Fetch Photos Framework
Instagram Firebase
To make our application look even better, we'll build a custom Photo Selector that displays photos onto a grid just like what is found on the Instagram app. This is quite simple and straightforward using the new Photos framework that is now available on iOS 8 and above.

Comments (30)
Hen Shabat
6 years ago
Why for the image you used "if let" and not "guard let"?
Brian Voong
6 years ago
Hen Shabat
6 years ago
Thanks!
weare99
6 years ago
Amazing Question. I already ask this question myself. Thanks Shabat
Roman
6 years ago
Hi! I have a question: Why you reload collectionView every time after appending image to array? We might simple reload it once after "imageManager.requestImage(for:..." function is done and adding all of images into array, Isn't it so?
Roman
6 years ago
I'm Sorry, i was wrong in the name of the function: allPhotos.enumerateObjects is right name.
Brian Voong
6 years ago
Nathan Hsiao
6 years ago
does the framework work on ios 9?
Brian Voong
6 years ago
Jesus Adolfo
6 years ago
How did you add photos to the simulator ?
Brian Voong
6 years ago
Francisco Arrieta
6 years ago
In the final app, how are you fetching more photos as the user scrolls down?
Brian Voong
6 years ago
josephlausf
6 years ago
That would be great!
anuj01112
5 years ago
Please provide algorithm/ steps you used to fetch image using lazyloading. It would be great help
cyn89110
4 years ago
Hi Brian, Is this problem solved ??
shender ramos
6 years ago
hey Brian thank you for all your tutorials, i've learn so much from watching you, as we go on i keep telling my self that 50 dollars for the course is nothing compare to all the value i am getting from it... i know most of the stuff from watching you on youtube but i've learn things here that i am very grateful you did this course... thank a lot man keep the good work thumbs up
Casey West
6 years ago
Is there a way to resize the photo with a zooming in/out? Currently it cuts off a bit of the photos actual size.
smiller193
6 years ago
Is anyone having an issue with the input accessory view getting stuck at the bottom? When exiting comment screen
dutchCode
6 years ago
I receive an error when clicking on the 'plus' tab. The code works fine for me until the following line: "allPhotos.enumerateObjects({ (asset, count, stop) in" Does this have to do with PHAssetSourceType or Icloud? Anyone who can help me out here? Thanks a lot in advance
Brian Voong
6 years ago
dutchCode
6 years ago
I got the regular 'signal SIGABRT,' -> 0_abort_with_payload , nothing in the console though.
Brian Voong
6 years ago
Daryl Wong
5 years ago
Add these two lines in info.plist: <key>NSPhotoLibraryUsageDescription</key> <string>${PRODUCT_NAME} PhotoLibrary Usage</string>
anasser2
5 years ago
This fixed the 'signal SIGABRT' error. Thanks
Chard
6 years ago
whats the different between " let some = {}() " and "lazy var some = {}()"
Goldor
4 years ago
Lazy var declaration allows you to use class variables inside of your closure. For instance: let colorForAllViews = UIColor.black lazy var someView: UIView = { let view = UIView() view.backgroundColor = colorForAllViews return view }() Using a "let" declaration would prevent you from using the "colorForAllViews" variable. You can think of "lazy var" as a way of saying that the element will only initialize itself once it is called upon by a function in the class (hence the lazy part), after all of the class variables have been initialized first. Otherwise, it wouldn't be able to access the class variables.
jalong4
6 years ago
Instead of adding a custom Collection cell, could you have just created a UIImageView in PhotoSelectorController.cellForItemAt and set it's image to the image from the array and add that UIImageView as a subView to cell.contentView? Or would this not work for some reason? I realize if you wanted a bunch of fields in the cell, a custom cell is required but since this is just one image, was wondering if this would. have worked as well. Thanks.
Brian Voong
6 years ago
jalong4
6 years ago
makes sense. Thanks for replying.
Kenny Ho
5 years ago
Anyone else having a problem sense they updated to Swift 4? The app isn't fetching images anymore. Idk what's wrong
Brian Voong
5 years ago
Ashim Dahal
5 years ago
I am having the same issue I couldn't fetch all the image beside the default images that are available... any reason why this might have happened... I have no consol error... Its just give me 5 images that are available...
Ashim Dahal
5 years ago
I found the solution... I had fetch limit as 5 .... its displaying good now
Adam El-Kadi
5 years ago
Hey Brian, I have a really important question. What email can I use to contact you, I need to talk to you about something important.
Brian Voong
5 years ago
arum seo
5 years ago
Hey Brian, First of all... great tutorial. Thanks for the hard work. Can you just guide me on how to implement (drag&scale) to the main PhotoSelectorHeader? Just like the current instagram app? Is there way to just subclass custom uiviewcontroller inside the header and do the editing function there? thanks.
Michael Evans
5 years ago
Hi Brian When I run the app the photo selector controller appears fairly rapidly in the simulator. If I run it on my phone (6s) it takes about 4 seconds to load the images. I maybe have 500 images. Is that expected and would it take that long on a production app?
Brian Voong
5 years ago
Michael Evans
5 years ago
Thanks Brian. Loving the course! I’m also seeing old images in the home fee cells before they get reused for the next image despite the fix you put in place to check if the URL != lasturl. Is that a sim problem or something else? I copied your code for customImageView and loadImage() Thanks again
Brian Voong
5 years ago
Michael Evans
5 years ago
Cheers
Jaylon22
5 years ago
Hi Brian When i choose the photoselector button the images sometimes shuffle out of place once i cancel and click it again please help. Here is my code var selectedImage: UIImage? var images = [UIImage]() var assets = [PHAsset]() fileprivate func assetFetchOptions() -> PHFetchOptions { let fetchOptions = PHFetchOptions() fetchOptions.fetchLimit = 30 let sortDescriptor = NSSortDescriptor(key: "creationDate", ascending: false) fetchOptions.sortDescriptors = [sortDescriptor] return fetchOptions } fileprivate func fetchPhotos(){ let allPhotos = PHAsset.fetchAssets(with: .image, options: assetFetchOptions()) DispatchQueue.global(qos: .background).async { allPhotos.enumerateObjects({ (asset, count, stop) in let imageManager = PHImageManager.default() let targetsize = CGSize(width: 200, height: 200) let options = PHImageRequestOptions() options.isSynchronous = true imageManager.requestImage(for: asset, targetSize: targetsize, contentMode: .aspectFit, options: nil, resultHandler: { (image, info) in if let image = image{ self.images.append(image) self.assets.append(asset) if self.selectedImage == nil{ self.selectedImage = image } } if count == allPhotos.count - 1{ DispatchQueue.main.async { self.collectionView?.reloadData() } } }) }) } }
Jaylon22
5 years ago
The images are not sorted correctly by the creation date. just piggy backing on the question i had.
Jaylon22
5 years ago
Actually i figured it out options wasn't supposed to be nil.
inovacion
5 years ago
Hi Brian I followed your instructions very carefully and I used as a limitation the number 9. But it seems like I have a problem, because in the images array, when I print images.count I get the value of 18. What could be the problem.
inovacion
5 years ago
I found the solution... I forgot to add: options.isSynchronous = true
Jaylon22
5 years ago
Hey brian, Quick question i search around but did not seem to find out information about it. Does xcode (or apple rather) has a import videos feature to make a video gallery?
Goldor
4 years ago
I guess you could set the PHAssetMediaType below to .video instead of .image: PHAsset.fetchAssets(with: PHAssetMediaType, options: PHFetchOptions?)
ducward
5 years ago
Does anyone have a solution to fetch photos if the photos are stored on icloud and not locally? Thanks in advance.
ducward
5 years ago
options.isNetworkAccessAllowed = true is set but the icloud images are not getting set to the image view in the header.
Boula
5 years ago
Hey Brian, Wonderful Video (as usual) ... But i have question about what is the meaning of target size to be of CGSize(width: 250, height: 250) ?!
lbta
5 years ago
shubhamsaurav
5 years ago
m not able to use anchor property in my photoImageView
Brian Voong
5 years ago
shubhamsaurav
5 years ago
Thread 1: Fatal error: init(coder:) has not been implemented ... m getting dz error in photoSelector cell
Brian Voong
5 years ago
roygbiv
4 years ago
Hi Brian, Not sure but is there any difference between images[indexPath.item] vs images[indexPath.row] ? I ask because some books/youtubers use row while others use items
Brian Voong
4 years ago
elrafael16
4 years ago
Hi Brian, I really hope you could answer my question. I was trying to implement ASDK with this project but can't seem to make it work. When getting the indexPath for the posts coming from firebase I get an error that say the posts from firebase has an index out of range or I believe is empty. What am I doing wrong? This is the original func for getting the cellForItemAtindexPath override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! HomePostCell cell.post = posts[indexPath.item] cell.delegate = self return cell } This is for the ASDK func collectionNode(_ collectionNode: ASCollectionNode, nodeForItemAt indexPath: IndexPath) -> ASCellNode { // This is where I get the error let cell = posts[indexPath.item] let cellNode = { () -> ASCellNode in return ImageCellNode(post: cell) } return cellNode() }
Daniel Peach
4 years ago
If you just present the custom photo picker before knowing if you have permission to access photos yet, you will have to accept, and then cancel, and then re-open it in order to see the photos. If you check first, however, you can open the custom photo picker only if you know you have permission already. User this in the MainTabBarController instead: PHPhotoLibrary.requestAuthorization { (authStatus) in switch authStatus { case .authorized: DispatchQueue.main.async { self.present(navController, animated: true, completion: nil) } default: break } }
fkeebie
4 years ago
Photo framework I've learned from this tutorial is really new to me. I only know UIImagePickerController when I get the image from the photo library of my device. Thanks for your great, down-to-earth, enjoyable tutorial full of exciting stuff!
Han Htoo Naung001
4 years ago
Why it doesn’t see photo after first time click Allow button ?
Devwei
4 years ago
vimeo.com 拒绝了我们的连接请求。
sashensingh
4 years ago
I noticed a slight performance hit when we press the + button....this comes from the fetchPhotos function .... if i wrap this on the mainThread then its a lot faster however the collectionView images will get populated a split second later....in which case the user will see a blank collectionview then the images get set...any way around this?
sashensingh
4 years ago
nevermind already answers it in the next video :)
HELP & SUPPORT