Saving Image as Binary Data
Intermediate Training Core Data
Photos in UIKit are typically drawn using a UIImage object. However, Core Data doesn't support UIImage types as something we can save. Instead, let's use the underlying image data for our photo and save it as Binary Data in our Core Data Model. Once we're able to persist this successfully, our main list will need to be augmented to show these company images.

Comments (23)
Daniel Peach
5 years ago
OMG! I just spent over an hour trying to figure out why my images where rounded diamonds and not a circle! APPARENTLY if the initial image you choose (got my own from the internet) is 512x512, it makes the real photos really tiny rounded diamonds. (To duplicate this, download the project at this point, drag out the select_photo_empty@2x.png to you desktop, delete the select_photo_empty.imageset folder, resize the photo to 512x512, and drag it back into the assets folder) Why does swift do this? I figure it has to do with constraints and the resizing once we choose an image, because the actual image is fine, but when we update it with a custom image, it is very tiny and not a circle. If you know why it behaves this way please let me know.
RichyCode
5 years ago
had the same thing, easiest fix in the world....just resize the image and use content mode for scaling, works like a dream
forsuresh
5 years ago
Running into this same issue. Do you resize the image though code, or just drag it out to the desktop, adjust size and then drag it back into your project?
nerddd
5 years ago
Hey Brian, Why did you create a separate function where you configure circle imageview instead of setting all of that right inside companyImageView initialisation at the top? Is there any difference between your approach and the one i said?
jesseIOS17
5 years ago
Great lesson, great course.
cesare
5 years ago
This is a great lesson and this course is very worth it but I have a question: so when the user saves a company to which we haven't set an image (there's just the circular select photo placeholder) the app is going to save the placeholder right image? not sure if this is something we want. how do we fix this? should we use a boolean to indicate whether the user has selected an image and if yes then save the companyImageView.image?
Brian Voong
5 years ago
magic
5 years ago
Hey Brian, regarding the circular image in CreateCompanyController, couldn't we set the corner radius and other properties inside imageview initialization?
Brian Voong
5 years ago
magic
5 years ago
Ohh i got it! Thank you :)
Stryfe
5 years ago
Hi Brian, strange issue, when selecting a picture that is smaller than the selection box, when I save and we go to the company list, the thumbnail image is actually bigger than the square I was expecting. If I go back and edit the company and the image and zoom in on the photo so that it completely fills the selection box and save, the thumbnail image is now a square and correct.
m.k.hasson97
5 years ago
Hello Brain, in swift 4.2 the(UIImageJPEGRepresentation' has been replaced by instance method 'UIImage.jpegData(compressionQuality:)) how can i fix that, i tried to convert the app to swift 4 but it doesn't work
Brian Voong
5 years ago
m.k.hasson97
5 years ago
i have converted the project to swift 4, now it's work
harehman83
5 years ago
Hi Brian, how to save circular image from CreateCompanyController in the CompanyController cell as I tried to to this but it didn't work; cell.imageView?.layer.cornerRadius = cell.imageView?.frame.width / 2 cell.imageView?.layer.cornerRadius = cell.imageView?.frame.height / Haaris cell.imageView.clipToBounds = true
dclawson
5 years ago
Engineering process question: I've noticed that you typically build out these projects, step by step and test them at each incremental feature change. I think that's an awesome idea because you're always sure that pieces of it working, and can probably quickly find where things went wrong. Do you use the same process when you're working for a client? For example: the table view cells start out with simple text, then more text, and now include images. Would you ever just build the entire cell to its final state (more or less) and then wire it up? Build the entire model first?
Brian Voong
5 years ago
dniswhite
5 years ago
when you are saving image data you aren't accounting for the fact that the user doesn't have to select an image. when they don't select an image it uses the "empty image" as its image. this causes some weird and unexpected results when displaying that as the image for the company. I fixed with a state variable that lets me know when an image was selected.
forsuresh
5 years ago
Perhaps a bug in download project code? When I change the hard-coded anchors concerning companyImageView from 100 to 200 & leave everything the same: - CompanyImageView shows as a rounded rectangle. companyImageView.heightAnchor.constraint(equalToConstant: 200).isActive = true companyImageView.widthAnchor.constraint(equalToConstant: 200).isActive = true imagePickerController(...didFinishPickingMediaWithInfo...) still works properly and leaves CompanyImageView as a perfect circle. === Issue occurs even if I comment out all other UI components so CompanyImageView is alone on the screen.
forsuresh
5 years ago
This issue only shows itself if you get to this screen after selecting a row from tableView.
seekerlk
4 years ago
This is what worked for me in Swift 4.2 let imageData = image.jpegData(compressionQuality: 1)! as NSData
Christophe Bugnon
4 years ago
Hey Brian, the reason why it doesn’t asked you to confirm access for picture is beaucause you already confirm it before. You have to reset your simulator from bar task or uninstall and install again your application. Hope it works for you !
Haohong Zhao
4 years ago
Hi Brian, I put the "setupCircularImageStyle()" inside company's "didSet()" method as you did, but the image is still square when I enter the editView. The image becomes round after I select an image from the photoPickerView. I set a breakpoint in the company's "didSet()" method, and see that the "setupCircularImageStyle()" is executed. I use Xcode 10 and macOS Mojave. Can you please give an advice on how to further debug this problem? Many thanks!
Brian Voong
4 years ago
Haohong Zhao
4 years ago
To debug further, I print companyImageView's borderWidth, borderColor and clipToBound property in "handleSelectPhoto". I see that they are set correctly, but the image does not become round. Is this a bug related to Xcode simulator?
Brian Voong
4 years ago
Haohong Zhao
4 years ago
Yeah. Thank you, Brian! The corner radius is still 0. The problem is that when "didSet()" is executed, the companyImageView does not have a size. I print companyImageView's frame width right after I set up auto-layout constraints for companyImageView, and I see a 0 here as well.
Haohong Zhao
4 years ago
I am confused where the companyImageView get its size. I print its frame width in "handleSelectPhoto()", and the companyImageView has a width. So, the companyImageView should get its size after company's "didSet()" --> "setupUI()"(where it gets auto-layout constraints).
Haohong Zhao
4 years ago
Hi Brian, I had a discussion with my TA and found that from viewDidLayoutSubview, the companyImageView got its size correctly(100 px). The lifecycle of the controller is company didSet() -- > viewDidLoad() --> viewWillAppear() --> viewDidLayoutSubview() --> viewDidAppear(). Even its auto-layout constraints are set in viewDidLoad(), the companyImageView does not have a correct size before viewDidLayoutSubview().
Brian Voong
4 years ago
xuhua
4 years ago
请问你这里具体是如何解决的呢?
werg09
4 years ago
Great tutorial Brian, small update here, 3:13 let imageData = companyImage.jpegData(compressionQuality: 0.8) I'm Having a problem with setupCircularImageStyle( ) When i put setupCircularImageStyle( ) inside of my didSet method , it makes my Image disappear when i press on edit from the tableview. I can still touch it to activate the ImagePicker and everything works like normal. If i do not putting that setupCircularImageStyle( ) method into the didSet method everything works like normal but i get a square picture when i hit edit. thanks for any help.
Brian Voong
4 years ago
werg09
4 years ago
Its just my code that is off somewhere. I ran your project and it works fine. I must have made a mistake somewhere meanwhile i was following along. Any idea where I might look ? Ill continue looking, Thanks!
werg09
4 years ago
I found it, it has to do with the default image i was using inside of the closure in the CreateCompanyController. I was using some other random image, not the same one as you. companyImageView : UIImageView = { let imageView = UIImageView(image: IMAGE) }() once i would hit edit , the next viewController would pop up and the picture on that controller was turning blank for some reason . Its good now .
werg09
4 years ago
If you decide to use a random small image as i did you want to get make your own frame for the image inside of setupCircularImageStyle() method. Then once you go to edit your company it does indeed use a circular image to display it. companyImageView.frame = CGRect(x: 0, y: 0, width: 100, height: 100) at least this worked for me. PS i finished the course... Its truly worth every Penny.
Beetroot
4 years ago
thank you!
Caroline Kuo
4 years ago
Thank you Brian for this course. I'm lovin it! Aloha
jdhindsa
4 years ago
Hi Brian, when I click on an image to add to a company, the image gets selected and the app goes back to the Create Company screen. I have no option to edit/crop/pan the image like you were showing in your videos. Is there a specific function call I need to implement to do this?
jdhindsa
4 years ago
UIImageJPEGRepresentation didn't work. Core Data seems to expecting an NSData object not a Data object. I used this code instead in createCompany() to save the image: if let companyImage = companyImageView.image { if let data = companyImage.jpegData(compressionQuality: 0.8) as NSData? { company.setValue(data, forKey: "imageData") } }
jademagnum
3 years ago
if let companyImage = companyImageView.image { let imageData = companyImage.jpegData(compressionQuality: 0.8) company?.imageData = imageData }
arroyot24
3 years ago
This method works for me.
jdhindsa
4 years ago
Another question Brian, my image view in the Create Company screen is the shape of an eye, not a circle. What causes this?
Afonso Henrique S. Sabino
4 years ago
Hi Brian, After the code below may image in Create Company View get the shape like a diamond, not a circle. I am using the swift 5. Do you know what happened? Follow the code: var company: Company? { didSet{ nameTextField.text = company?.name guard let founded = company?.founded else {return} datePicker.date = founded if let imageData = company?.imageData { companyImageView.image = UIImage(data: imageData) setupCircularImageStyle() } } } private func setupCircularImageStyle() { companyImageView.layer.cornerRadius = companyImageView.frame.width / 2 companyImageView.clipsToBounds = true companyImageView.layer.borderColor = UIColor.darkBlue?.cgColor companyImageView.layer.borderWidth = 2 }
Afonso Henrique S. Sabino
4 years ago
I found the answer in previous questions... The problem is .... I had a discussion with my TA and found that from viewDidLayoutSubview, the companyImageView got its size correctly(100 px). The lifecycle of the controller is company didSet() -- > viewDidLoad() --> viewWillAppear() --> viewDidLayoutSubview() --> viewDidAppear(). Even its auto-layout constraints are set in viewDidLoad(), the companyImageView does not have a correct size before viewDidLayoutSubview(). By Haohong Zhao Do I forgot any thing?
Cinquain
4 years ago
Great work
Christopher J. Roura
3 years ago
Hi Brian, sorry for always commenting but I found out some more information on the photo library permissions. Apparently, the swift team has decided that for photo usage from the photo library specifically it is all user dependent. Apps no longer have access to photos unless if the user interacts with the device and select the photos. So basically it is no longer needed because the user selects the image they want to be shown thus "kind of" agreeing to that specifics photo usage. This is what I have found within my research through both Apple's documentation and stack overflow. I am not sure 100% of this but it's my best guess as to why it isn't needed anymore.
Manish Prakharan
3 years ago
cool stuff cool stuff
HELP & SUPPORT