An error occurred while trying to save an image in NSUSERDEFAULTS

When I tried to save an image in NSUserDefaults, the application crashed due to an error.

Why? Is it possible to save images using NSUserDefaults? If not, then how to save the image?

Picture…

Code…

var image1:UIImage = image1


var save1: NSUserDefaults = NSUserDefaults.standardUserDefaults()

save1.setObject(Info.Image1, forKey: "Image1")

save1.synchronize()

< p>Log error…

libc abi.dylib: Terminated with an uncaught exception of type NSException
(LLDB)

< /div>

NSUserDefaults is not just a big truck, you can throw anything you want. This is a series of tubes with only specific types.

You can save to NSUserDefaults:

> NSData
> NSString
> NSNumber
> NSDate
> NSArray
> NSDictionary

If you try To save any other content to NSUserDefaults, you usually need to archive it to an NSData object and store it (remember, you need to unarchive it later when you need it).

There are two ways to save The UIImage object is converted into data. There is a function for creating a PNG representation of an image or a JPEG representation of an image.

For PNG:

let imageData = UIImagePNGRepresentation (yourImage)

For JPEG:

let imageData = UIImageJPEGRepresentation(yourImage, 1.0)

The second parameter is the representation Compression quality CGFloat, where 0.0 is the lowest quality and 1.0 is the highest quality. Remember, if you use JPEG, every time you compress and decompress, if you use anything other than 1.0, then with Over time will reduce the quality. PNG is lossless, so you will not reduce the image quality.

To extract the image from the data object, UIImage has an init method to perform this operation:

< p>

let yourImage = UIImage(data:imageData)

No matter how you convert the UIImage object into data, this method will work.

When I tried to save the image in NSUserDefaults, the application crashed because of the error.

Why? Is it possible to save images using NSUserDefaults? If not, then how to save the image?

Picture…

Code…

var image1:UIImage = image1


var save1: NSUserDefaults = NSUserDefaults.standardUserDefaults()

save1.setObject(Info.Image1, forKey: "Image1")

save1.synchronize()

< p>Log error…

libc abi.dylib: Terminated with an uncaught exception of type NSException
(LLDB)

NSUserDefaults Not just a big truck, you can throw anything you want. This is a series of tubes with only specific types.

You can save to NSUserDefaults:

> NSData
> NSString
> NSNumber
> NSDate
> NSArray
> NSDictionary

If you try to save anything else to NSUserDefaults, you usually need to Archive it to an NSData object and store it (remember, you need to unarchive it later when you need it).

There are two ways to convert a UIImage object into data. There is a method for creating an image The function of PNG representation or JPEG representation of an image.

For PNG:

let imageData = UIImagePNGRepresentation(yourImage)

For JPEG :

let imageData = UIImageJPEGRepresentation(yourImage, 1.0)

The second parameter is the CGFloat representing the compression quality, where 0.0 is the lowest quality and 1.0 Is the highest quality. Remember, if you use JPEG, every time you compress and decompress, if you use anything other than 1.0, the quality will decrease over time. PNG is lossless, so you won’t Reduce the image quality.

To extract the image from the data object, UIImage has an init method To perform this operation:

let yourImage = UIImage(data:imageData)

No matter how you convert the UIImage object into data, this method will work Function.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4165 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.