I am trying to retrieve an image saved using:
let pngData = UIImagePNGRepresentation(image)
let finalPath = filePath() // This method just create the path to save to.
saveImage(pngData, filePath: finalPath)
Later I want to retrieve this data and set the UIImageView of a UIButton. However when I use the following code, it just displays a completely blue image.
Here is the code:
let filePath = tempCard?.frontPhoto
let imgData = UIImage(contentsOfFile: filePath!)frontPhotoButton.setImage(imgData, forState: .Normal)
frontPhotoButton.setImage(imgData, forState: .Normal)
I am not sure why this is just showing a blue button.
Edit:
I have also tried:
let filePath = tempCard?.frontPhoto
let imageData = NSData(contentsOfFile: filePath!
let image = UIImage(data: imageData!)
frontPhotoButton.setImage(image, forState: .Normal)
Same result.
For anyone else who has a problem in the future setting the background image of a UIButton. Both of the above code snippets work to retrieve the image.
The Problem was, in interface builder my button was: Type - System. When I changed Type - Custom everything worked fine.
Use this method to retrieve image from document directory of an app
func loadImageFromPath() {
dispatch_async(dispatch_get_main_queue(), {
let fileManager = NSFileManager.defaultManager()
var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
var getImagePath = paths.stringByAppendingPathComponent("Image2.png")
if (fileManager.fileExistsAtPath(getImagePath))
{
println("FILE AVAILABLE AT \(paths)");
//Pick Image and Use accordingly
var imageis: UIImage = UIImage(contentsOfFile: getImagePath)!
let data: NSData = UIImagePNGRepresentation(imageis)
self.imgProfileView.image = UIImage(data: data)
self.imgProfileView.contentMode = UIViewContentMode.ScaleAspectFit
}
else
{
println("FILE NOT AVAILABLE");
}
});
}
Related
I am using ImagePicker with SwiftUI by UIImagePickerController. It can select an Image. But I want to know the original image file name also. Is there any way to do this?
I already tried this using following codes:
if let url = info[UIImagePickerController.InfoKey.imageURL] as? URL {
imagePicker.fileName = url.lastPathComponent
imagePicker.fileType = url.pathExtension
}
But above code gives the temporary image file name.
You can use the originalImage attribute if you need to access the picked UIImage:
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
...
}
I tried but I couldn't get file name (url) from UIImage.
Following code solves the problem temporarily.
if let url = info[UIImagePickerController.InfoKey.referenceURL] as? URL {
let assets = PHAsset.fetchAssets(withALAssetURLs: [url], options: nil)
if let firstAsset = assets.firstObject,
let firstResource = PHAssetResource.assetResources(for: firstAsset).first {
imagePicker.fileName = firstResource.originalFilename
} else {
imagePicker.fileName = "IMG_random_string"
}
} else {
imagePicker.fileName = "IMG_random_string"
}
But here referenceURL and fetchAssets are depreciated. I tried with PHPickerViewController, but again couldn't find how to get selected image file name from it.
Thanks! :))
Writing to the file system allows users to write and retrieve string, image, etc., files to and from the application on their devices.
The images I write and retrieve to the app that appear when I run the simulator, for example, are unique to that app bundle, and differ from the images I write and retrieve from my device.
When I research how to write and read image files with Realm I am told not to save the image but the path. Images are too large to store. Got it.
But the file path I am saving naturally returns data native to the specific app bundle-- or returns nil. How might I write and retrieve image files/paths to and from Realm if the file path is unique to the device(s)? Do I need a separate image array?
First View Controller
let planet = planets[indexPath.item]
cell.name.text = planet.name
cell.system.text = planet.system
let imageData = (self.getDirectoryPath() as NSString).appendingPathComponent("image")
if self.fileManager.fileExists(atPath: imageData) {
// Populates every cell with the same image
cell.earthImage.image = UIImage(contentsOfFile: imageData)
// returns nil
cell.earthImage.image = UIImage(contentsOfFile: planet.image)
}
// Retrieve directory path for image
func getDirectoryPath() -> String {
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]
return documentsDirectory
}
Second View Controller
let planet = Planet()
self.realm = try! Realm(configuration: config(user: SyncUser.current!))
planet.name = self.name.text!
planet.system = self.system.text!
let imageData = (self.getDirectoryPath() as NSString).appendingPathComponent("person")
if self.fileManager.fileExists(atPath: imageData) {
self.earthImage.image = UIImage(contentsOfFile: imageData)
// Why does this not write to realm?
planet.image = imageData
}
try! self.realm.write {
self.realm.add(planet)
}
Here, I am posting from the secondViewController where I would like to render these values in my collectionView in the firstViewController's numberOfItemsInSection_:) method.
Here's the model:
class Planet: Object {
#objc dynamic var name = String()
#objc dynamic var system = String()
#objc dynamic var image = String()
}
i have a bunch of images in my App's Documents Directory. i want to load one of them in an UIImage that i have in my view. this is what i do:
myImage.image = UIImage(named: "image.jpg") // the file exist but this returns nil
I also tried to init the image using:
myImage.image = UIImage(contentsOfFile: imagePath) //this also doesn't work but the path i got starts (at least in the sim) with file://
Anyone can suggest something? I'm kinda a noob in iOS programming.
Thanks
EDIT:
imagePath comes from:
func getImgPath(name: String) -> String
{
let documentsDirectory = self.getDocumentsDirectory()
let fullpath = documentsDirectory.appendingPathComponent(name)
return fullpath.absoluteString
}
This might help
let documentDirectory = FileManager.SearchPathDirectory.documentDirectory
let userDomainMask = FileManager.SearchPathDomainMask.userDomainMask
let paths = NSSearchPathForDirectoriesInDomains(documentDirectory, userDomainMask, true)
if let dirPath = paths.first
{
let imageURL = URL(fileURLWithPath: dirPath).appendingPathComponent("name.png")
let image = UIImage(contentsOfFile: imageURL.path)
// Do whatever you want with the image
}
Try adding your images under Assets.xcassets. Then you'll be able to reference your image by:
UIImage(named: 'yourImageName')
You can conveniently add images with various size settings.
You can get the path of the image as fowllows:
let imagePath = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true).appendingPathComponent("image.jpg")
If this doesn't work, check if your file name is correct.
I understand that if you are downloading images from internet means, you need to convert the URL to UIImage
if let url = URL(string: "your url") {
let data = try? Data(contentsOf: url)
if let imageData = data {
if let image = UIImage(data: imageData) {
imageView.image = image
}
}
}
If i understand your question correct, this will be the answer.
I need to found an alternative of this method to save images
let save = UserDefaults.standard
let imageData = UIImageJPEGRepresentation(Photo.image!, 1.0)
save.set(imageData, forKey: "Image")
save.synchronize()
if let imgData = save.object(forKey: "Image"){
let compressedJPGImage = UIImage(data: imgData as! Data)
}
and load images
let imgData = save.object(forKey: "Image")
let compressedJPGImage = UIImage(data: imgData as! Data)
Photo.image = compressedJPGImage
The problem with this method is that i have a lot of another value saved with UserDefaults.standard so it take a lot of time (5-10 minutes) when i synchronize.
It is not advisable to save large files like images to UserDefaults. UserDefaults was intended to save very small data such as a user's preferred theme color of your app. Perhaps a suitable alternative is to save your images in the document directory. Here is a function that will allow you save an image:
func saveImage(image: UIImage) -> String {
let imageData = NSData(data: UIImagePNGRepresentation(image)!)
let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
let docs = paths[0] as NSString
let uuid = NSUUID().uuidString + ".png"
let fullPath = docs.appendingPathComponent(uuid)
_ = imageData.write(toFile: fullPath, atomically: true)
return uuid
}
The above function will create the name of the saved image for you. If you prefer to specify the name of the image you are saving then you could do the following (but you will be responsible for ensuring the image names you specify are unique):
func saveImage(image: UIImage, withName name: String) {
let imageData = NSData(data: UIImagePNGRepresentation(image)!)
let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
let docs = paths[0] as NSString
let name = name
let fullPath = docs.appendingPathComponent(name)
_ = imageData.write(toFile: fullPath, atomically: true)
}
To retrieve those images, you could pass the image name to this function:
func getImage(imageName: String) -> UIImage? {
var savedImage: UIImage?
if let imagePath = getFilePath(fileName: imageName) {
savedImage = UIImage(contentsOfFile: imagePath)
}
else {
savedImage = nil
}
return savedImage
}
Which relies on this function to work:
func getFilePath(fileName: String) -> String? {
let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
var filePath: String?
let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
if paths.count > 0 {
let dirPath = paths[0] as NSString
filePath = dirPath.appendingPathComponent(fileName)
}
else {
filePath = nil
}
return filePath
}
Here is an example of how you would now save your images instead of UserDefaults. I am saving an image I will call "Image":
saveImage(image: Photo.image, withName name: "Image")
Here is an example of how I would retrieve the saved image:
if let theSavedImage = getImage(imageName: "Image") {
//I got the image
}
UserDefaults is a place to store a small portion of data like user preferences. UserDefaults has very limited space and can be quite slow. In your case, you mentioned it's 5-10 minutes which I doubt though.
If you want images to be stored across the sessions of the app (persistent storage), you should consider using file system (Application_Folder/Library/Cache/) or Core Data framework. You will get better performance here while accessing the image.
If images are not needed to be persisted and need to be stored for a single session of the app, you should use the imageNamed: API of UIImage class. This API loads the image once in the memory and keeps it in the system cache. For all the successive accesses it refers to the cached image only. This will increase the system cache size and application's memory footprint if you are loading too many images. Another API is imageWithContentsOfFile:. Unlike the first API, this API will always load the new image instance in memory. Memory will be deallocated once image instance is released which is not the case with the first API.
Looking through the forums I have found that this issue is one that shows its head every now and then. And apparently doing so in a wide scope of different cases. None that I have managed to find seem to be similar to mine though.
I'm halfway through my program (lesson) in creating a usable twitter application. Testing it currently runs the program as it should, without any errors. But when I select an account the program crashes and an error message shows up at the image method which is supposed to load the avatar of the selected user. I assume that it is not able to retrieve a valid image or fetch data at all (because of the line ImageData = (NSDATA?) nil in the debug area), but I am by no means sure of anything, let alone how to or where to find a solution. If I am searching with the wrong keywords then please let me know. (I am searching for exc_bad_instruction and uiimage error) Thanks in advance.
I'll post the snippet of code where the issue presents itself below and what is shown in the debug area below that.
if let cachedImage = image {
cell.tweetUserAvatar.image = cachedImage
}
else {
cell.tweetUserAvatar.image = UIImage(named: "camera.png")
queue?.addOperationWithBlock() {
let imageURL = NSURL(string: imageURLString) as NSURL!
let imageData = NSData(contentsOfURL: imageURL) as NSData?
let image = UIImage(data: imageData!) as UIImage? // EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subode=0x0)
if let downloadedImage = image {
NSOperationQueue.mainQueue().addOperationWithBlock(){
let cell = tableView.cellForRowAtIndexPath(indexPath) as! TweetCell
cell.tweetUserAvatar.image = downloadedImage
}
self.imageCache?.setObject(downloadedImage, forKey: imageURLString)
}
}
}
Debug area:
imageURLString String
"http://pbs.twimg.com/profile_images/465756113825067008/8jH2nZO0_normal.png"
tableView UITableView 0x00007fc52481b400
indexPath NSIndexPath * 0xc000000000000016
self Chapter7_8___Social_App.FeedViewController 0x00007fc5235f5ef0
imageURL NSURL! "http://pbs.twimg.com/profile_images/465756113825067008/8jH2nZO0_normal.png"
imageData NSData? nil None
image UIImage? 0x000000010ee778dd
downloadedImage UIImage
I had this issue as well and found that my image was being initialized with bad data. This is to say that the image I requested from the server did not exist and the server sent back a response which could not be converted into an UIImage.
To mitigate this you can do the following:
if let cachedImage = image {
cell.tweetUserAvatar.image = cachedImage
}
else {
cell.tweetUserAvatar.image = UIImage(named: "camera.png")
queue?.addOperationWithBlock() {
let imageURL = NSURL(string: imageURLString) as NSURL!
let imageData = NSData(contentsOfURL: imageURL) as NSData?
if let image = UIImage(data: imageData!) {
let downloadedImage = image
NSOperationQueue.mainQueue().addOperationWithBlock(){
let cell = tableView.cellForRowAtIndexPath(indexPath) as! TweetCell
cell.tweetUserAvatar.image = downloadedImage
}
self.imageCache?.setObject(downloadedImage, forKey: imageURLString)
}
}
}
What I have done above is changed
let image = UIImage(data: imageData!) as UIImage?
if let downloadedImage = image {
...
...
}
To:
if let image = UIImage(data: imageData!) {
let downloadedImage = image
...
...
}
In this way I have checked that the image was able to be created successfully. If the image is not able to be created then the code will not execute and you will not receive an error.
If you expect an image to be at the url you specified in 'imageURLString' then I would suggest that you check the url you are using.
I also noticed that you did not get any data back which is why you could not create the UIIMage. You can test to see if this is the case with the following:
let imageData = NSData(contentsOfURL: imageURL) as NSData?
if imageData != nil {
Do More Stuff
...
{
I hope my answer was helpful. If you have any questions feel free to leave a comment and I'll do my best to answer them.
More Info Here:
This question & answer also provides methods on how to handle the case when the UIImage cannot be created from the data provide.