I need to be able to set the background image for this button. I need to store this so after the app closes the background image is the same.
eventsFirstButton.backgroundColor = UIColor(patternImage: UIImage(named: "events")!)
You could just save the state:
Correct answer:
UserDefaults.standard.set(true, forKey: "TestAnswer1")
//If the answer is incorrect set to false
On load:
if UserDefaults.standard.bool(forKey: "TestAnswer1") {
view.backgroundColor = UIColor.green
// or any other logic
} else {
view.backgroundColor = UIColor.red
// or any other logic
}
It's better to save it as base64string, you don't want to store large value to UserDefaults.
To encode UIImage use this:
let image = UIImage()
let data = image.pngData()
let imageBase64String = data?.base64EncodedString()
UserDefaults.standard.set(imageBase64String, forKey: "encodedImage")
And for decoding and retrieving UIImage use this:
if let imageBase64String = UserDefaults.standard.value(forKey: "encodedImage"),
let url = URL(string: String(format:"data:application/octet-stream;base64,%#",imageBase64String))
{
do
{
let data = try Data(contentsOf: url)
let image = UIImage(data: data)
}
catch let error
{
print("Error decoding image")
}
}
If you really need to save the PNG, JPEG images locally, use CoreData to store them on the device.
You can use UserDefaults to save your image
Save
if let image = eventsFirstButton.imageView?.image {
let imageData = image.pngData()
UserDefaults.standard.set(imageData, forKey: "imageData")
}
Retrieve
if let imageData = UserDefaults.standard.data(forKey: "imageData") {
print("IMG data: ", imageData)
// your code here
}
Related
I'm using this code in CellForRowAt for showing image. Scrolling is smoothly but network debug says me that it still download image every time that i scroll the table.
How can I work for download all the images once?
if let url = URL( string: rest1.image) {
DispatchQueue.global().async {
if let data = try? Data(contentsOf: url) {
DispatchQueue.main.async {
cell.RestaurantImage.image = UIImage(data: data)
}
}
}
}
You need to use NSCache for saving and retrieving images. Once the images are fetched from network store it inside the Cache and from the next time load the images from the Cache. Create an instance of NSCache with keys NSString and value NSData because NSCache only allows class types. Here's an example:
Create an image cache outside the cellForItem method, or you can create it as Global, like this:
let imageCache = NSCache<NSString, NSData>()
And then in cellForItem method:
if let url = URL(string: rest1.image) {
if let data = imageCache.object(forKey: rest1.image as NSString) {
cell.RestaurantImage.image = UIImage(data: data as Data)
} else {
DispatchQueue.global().async {
if let data = try? Data(contentsOf: url) {
imageCache.setObject(data as NSData, forKey: rest1.image as NSString)
DispatchQueue.main.async {
cell.RestaurantImage.image = UIImage(data: data)
}
}
}
}
}
I am needing to load images from a URL and store them locally so they dont have to be reloaded over and over. I have this extension I am working on:
extension UIImage {
func load(image imageName: String) -> UIImage {
// declare image location
let imagePath: String = "\(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])/\(imageName).png"
let imageUrl: URL = URL(fileURLWithPath: imagePath)
// check if the image is stored already
if FileManager.default.fileExists(atPath: imagePath),
let imageData: Data = try? Data(contentsOf: imageUrl),
let image: UIImage = UIImage(data: imageData, scale: UIScreen.main.scale) {
return image
}
// image has not been created yet: create it, store it, return it
do {
let url = URL(string: eventInfo!.bannerImage)!
let data = try Data(contentsOf: url)
let loadedImage: UIImage = UIImage(data: data)!
}
catch{
print(error)
}
let newImage: UIImage =
try? UIImagePNGRepresentation(loadedImage)?.write(to: imageUrl)
return newImage
}
}
I am running into a problem where the "loadedImage" in the UIImagePNGRepresentation comes back with an error "Use of unresolved identifier loadedImage". My goal is to store a PNG representation of the image locally. Any suggestions on this error would be appreciated.
It's a simple matter of variable scope. You declare loadedImage inside the do block but then you attempt to use outside (after) that block.
Move the use of loadedImage to be inside the do block.
You also need better error handling and better handling of optional results. And your load method should probably return an optional image incase all attempts to get the image fail. Or return some default image.
Here's your method rewritten using better APIs and better handling of optionals and errors.
extension UIImage {
func load(image imageName: String) -> UIImage? {
// declare image location
guard let imageUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent(imageName).appendingPathExtension("png") else {
return nil // or create and return some default image
}
// check if the image is stored already
if FileManager.default.fileExists(atPath: imageUrl.path) {
if let imageData = try? Data(contentsOf: imageUrl), let image = UIImage(data: imageData) {
return image
}
}
// image has not been created yet: create it, store it, return it
do {
let url = URL(string: eventInfo!.bannerImage)! // two force-unwraps - consider better handling of this
if let data = try Data(contentsOf: url), let loadedImage = UIImage(data: data) {
try data.write(to: imageUrl)
return loadedImage
}
}
catch{
print(error)
}
return nil // or create and return some default image
}
}
If eventInfo!.bannerImage is a remote URL, then you must never run this code on the main queue.
I'm having problems cacheing for images from JSON correctly with this UIImageView extension. The images load correctly when I first open the app and scroll down the page. However when I scroll back up, they don't reload and are completely gone. Can anyone see anything wrong with the code?
let imageCache = NSCache<AnyObject, AnyObject>()
extension UIImageView {
func loadImageUsingUrlString(urlString: String) {
let url = NSURL(string: urlString)
if let imageFromCache = imageCache.object(forKey: urlString as AnyObject) as? UIImage {
self.image = imageFromCache
return
}
URLSession.shared.dataTask(with: url! as URL) { (data, response, error) in
if error != nil {
print(error ?? "URLSession error")
return
}
DispatchQueue.main.async {
let imageToCache = UIImage(data: data!)
imageCache.setObject(imageToCache!, forKey: urlString as AnyObject)
self.image = imageToCache
}
}.resume()
}
}
Here is the snippet from the cell.swift file
let imageCache = NSCache<AnyObject, AnyObject>()
func setupThumbnailImage() {
if let thumbnailImageUrl = television?.poster_url {
let urlPrefix = "https://www.what-song.com"
let urlSuffix = thumbnailImageUrl
let urlCombined = urlPrefix + urlSuffix
thumbnailImageView.loadImageUsingUrlString(urlString: urlCombined)
}
}
I suggest using kingFisher, it is very easy to use and it manages all starting from cache threads etc.
let imageResource = ImageResource(downloadURL:URL(string: imagePath )!,cacheKey: imagePath )
viewImage.kf.indicatorType = .activity
viewImage.kf.setImage(with: resource)
where imagePath is the url of your image and viewImage is your imageView
Most probably you would be calling it in wrong way.
Remember that in tableView you reuse the cells.
By the time response comes back for the URLSessionTask you would have already scrolled up/down. In that case self.image would be assigned to the currently visible cell.
Please add your cellForRow code in question.
I am trying to append image with its data in Array after downloading and then try to save in NSUserDefaults. But getting an error. I dont know what is proper way to save and read it .
Can anyone please tell me how i can do this?
Thanks
var imgIndex = 0
var imageArray : [UIImage] = []
typealias CompletionHandler = (image: UIImage) -> Void
downloadFileFromURL(NSURL(string: self.posts.objectAtIndex(indexPath.row).valueForKey("enclosure") as! String)!, completionHandler:{(img) in
dispatch_async(dispatch_get_main_queue(), { () -> Void in
cell.sideImageView.image = img
imageArray.insert(img, atIndex: self.imgIndex) //.append(img)
self.imgIndex++
print("Image append with data")
self.newsDefaults.setObject(imageArray, forKey: "image")
})
})
func downloadFileFromURL(url1: NSURL?,completionHandler: CompletionHandler) {
// download code.
if let url = url1{
let priority = DISPATCH_QUEUE_PRIORITY_HIGH
dispatch_async(dispatch_get_global_queue(priority, 0)) {
let data = NSData(contentsOfURL: url)
if data != nil {
print("image downloaded")
completionHandler(image: UIImage(data: data!)!)
}
}
}
}
You can't add images to user defaults, they aren't supported. You'll need to convert the image to data and save that instead (either to user defaults or, better, onto disk in a file...
imageArray.insert(UIImageJPEGRepresentation(img, 0.75), atIndex: self.imgIndex)
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.