storing background images in userdefaults
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")!)
ios swift userdefaults
add a comment |
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")!)
ios swift userdefaults
1
Where are you setting this? If you put this into viewDidLoad you dont have to save to UserDefaults. Also userdefaults is not the place to save large pieces of data.
– Alexander
Nov 15 '18 at 17:44
if a user has finished the test and got correct answers set the background color to green but if they have not finished the test set the color to red so if the color is green it would stay green to show they have finished the test and can try another one
– Alex
Nov 15 '18 at 18:11
add a comment |
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")!)
ios swift userdefaults
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")!)
ios swift userdefaults
ios swift userdefaults
edited Nov 15 '18 at 17:47
rmaddy
245k27324388
245k27324388
asked Nov 15 '18 at 17:34
Alex Alex
83
83
1
Where are you setting this? If you put this into viewDidLoad you dont have to save to UserDefaults. Also userdefaults is not the place to save large pieces of data.
– Alexander
Nov 15 '18 at 17:44
if a user has finished the test and got correct answers set the background color to green but if they have not finished the test set the color to red so if the color is green it would stay green to show they have finished the test and can try another one
– Alex
Nov 15 '18 at 18:11
add a comment |
1
Where are you setting this? If you put this into viewDidLoad you dont have to save to UserDefaults. Also userdefaults is not the place to save large pieces of data.
– Alexander
Nov 15 '18 at 17:44
if a user has finished the test and got correct answers set the background color to green but if they have not finished the test set the color to red so if the color is green it would stay green to show they have finished the test and can try another one
– Alex
Nov 15 '18 at 18:11
1
1
Where are you setting this? If you put this into viewDidLoad you dont have to save to UserDefaults. Also userdefaults is not the place to save large pieces of data.
– Alexander
Nov 15 '18 at 17:44
Where are you setting this? If you put this into viewDidLoad you dont have to save to UserDefaults. Also userdefaults is not the place to save large pieces of data.
– Alexander
Nov 15 '18 at 17:44
if a user has finished the test and got correct answers set the background color to green but if they have not finished the test set the color to red so if the color is green it would stay green to show they have finished the test and can try another one
– Alex
Nov 15 '18 at 18:11
if a user has finished the test and got correct answers set the background color to green but if they have not finished the test set the color to red so if the color is green it would stay green to show they have finished the test and can try another one
– Alex
Nov 15 '18 at 18:11
add a comment |
3 Answers
3
active
oldest
votes
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
add a comment |
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.
Do not store images in UserDefaults. That's not what it is for.
– rmaddy
Nov 15 '18 at 19:04
And why is it better to store the larger base64 string than the raw image data?
– rmaddy
Nov 15 '18 at 19:04
base64 string is not larger than raw image data files. I know that UserDefaults are not for saving images, I just posted a solution for it and said that it's better to store them using CoreData. Read my answer.
– Srdjan
Nov 16 '18 at 16:47
add a comment |
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
Do not store images in UserDefaults. That's not what it is for.
– rmaddy
Nov 15 '18 at 19:03
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325015%2fstoring-background-images-in-userdefaults%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
add a comment |
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
add a comment |
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
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
answered Nov 15 '18 at 18:57
AlexanderAlexander
13619
13619
add a comment |
add a comment |
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.
Do not store images in UserDefaults. That's not what it is for.
– rmaddy
Nov 15 '18 at 19:04
And why is it better to store the larger base64 string than the raw image data?
– rmaddy
Nov 15 '18 at 19:04
base64 string is not larger than raw image data files. I know that UserDefaults are not for saving images, I just posted a solution for it and said that it's better to store them using CoreData. Read my answer.
– Srdjan
Nov 16 '18 at 16:47
add a comment |
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.
Do not store images in UserDefaults. That's not what it is for.
– rmaddy
Nov 15 '18 at 19:04
And why is it better to store the larger base64 string than the raw image data?
– rmaddy
Nov 15 '18 at 19:04
base64 string is not larger than raw image data files. I know that UserDefaults are not for saving images, I just posted a solution for it and said that it's better to store them using CoreData. Read my answer.
– Srdjan
Nov 16 '18 at 16:47
add a comment |
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.
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.
answered Nov 15 '18 at 18:05
SrdjanSrdjan
799
799
Do not store images in UserDefaults. That's not what it is for.
– rmaddy
Nov 15 '18 at 19:04
And why is it better to store the larger base64 string than the raw image data?
– rmaddy
Nov 15 '18 at 19:04
base64 string is not larger than raw image data files. I know that UserDefaults are not for saving images, I just posted a solution for it and said that it's better to store them using CoreData. Read my answer.
– Srdjan
Nov 16 '18 at 16:47
add a comment |
Do not store images in UserDefaults. That's not what it is for.
– rmaddy
Nov 15 '18 at 19:04
And why is it better to store the larger base64 string than the raw image data?
– rmaddy
Nov 15 '18 at 19:04
base64 string is not larger than raw image data files. I know that UserDefaults are not for saving images, I just posted a solution for it and said that it's better to store them using CoreData. Read my answer.
– Srdjan
Nov 16 '18 at 16:47
Do not store images in UserDefaults. That's not what it is for.
– rmaddy
Nov 15 '18 at 19:04
Do not store images in UserDefaults. That's not what it is for.
– rmaddy
Nov 15 '18 at 19:04
And why is it better to store the larger base64 string than the raw image data?
– rmaddy
Nov 15 '18 at 19:04
And why is it better to store the larger base64 string than the raw image data?
– rmaddy
Nov 15 '18 at 19:04
base64 string is not larger than raw image data files. I know that UserDefaults are not for saving images, I just posted a solution for it and said that it's better to store them using CoreData. Read my answer.
– Srdjan
Nov 16 '18 at 16:47
base64 string is not larger than raw image data files. I know that UserDefaults are not for saving images, I just posted a solution for it and said that it's better to store them using CoreData. Read my answer.
– Srdjan
Nov 16 '18 at 16:47
add a comment |
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
Do not store images in UserDefaults. That's not what it is for.
– rmaddy
Nov 15 '18 at 19:03
add a comment |
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
Do not store images in UserDefaults. That's not what it is for.
– rmaddy
Nov 15 '18 at 19:03
add a comment |
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
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
answered Nov 15 '18 at 17:49
Tung Vu DucTung Vu Duc
321316
321316
Do not store images in UserDefaults. That's not what it is for.
– rmaddy
Nov 15 '18 at 19:03
add a comment |
Do not store images in UserDefaults. That's not what it is for.
– rmaddy
Nov 15 '18 at 19:03
Do not store images in UserDefaults. That's not what it is for.
– rmaddy
Nov 15 '18 at 19:03
Do not store images in UserDefaults. That's not what it is for.
– rmaddy
Nov 15 '18 at 19:03
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325015%2fstoring-background-images-in-userdefaults%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Where are you setting this? If you put this into viewDidLoad you dont have to save to UserDefaults. Also userdefaults is not the place to save large pieces of data.
– Alexander
Nov 15 '18 at 17:44
if a user has finished the test and got correct answers set the background color to green but if they have not finished the test set the color to red so if the color is green it would stay green to show they have finished the test and can try another one
– Alex
Nov 15 '18 at 18:11