Reputation: 41745
I can create UIImage
from NSData
using [UIImage imageWithData:]
or [UIImage initWithData:]
methods.
I wonder if I can get the NSData
back from an existing UIImage
?
Something on the line of NSData *myData = [myImage getData];
Upvotes: 112
Views: 70149
Reputation: 10146
This will get you the underlying data of a UIImage
, no png/jpeg conversion required.
extension UIImage {
var rawData: Data? {
guard let cgImage else { return nil }
let colorSpace = CGColorSpaceCreateDeviceRGB()
let context = CGContext(
data: nil,
width: cgImage.width,
height: cgImage.height,
bitsPerComponent: cgImage.bitsPerComponent,
bytesPerRow: cgImage.bytesPerRow,
space: colorSpace,
bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue
)
context?.draw(
cgImage, in: .init(
origin: .zero,
size: .init(
width: CGFloat(cgImage.width),
height: CGFloat(cgImage.height)
)
)
)
guard let data = context?.data else {
print("Failed to get image data")
return nil
}
return .init(bytes: data, count: cgImage.bytesPerRow * cgImage.height)
}
}
Upvotes: 1
Reputation: 4970
NSData *imageData = UIImageJPEGRepresentation(image, 0.7); // 0.7 is JPG quality
or
NSData *imageData = UIImagePNGRepresentation(image);
Depending if you want your data in PNG format or JPG format.
In modern versions of Swift, the methods above have been replaced with
let data = image.jpegData(compressionQuality: 0.7)
and
let data = image.pngData()
respectively.
Note that both return an optional value (Data?
).
Upvotes: 195
Reputation: 91891
The only solution I found to serialize/unserialize a UIImage (via Data) is by using this solution.
You can then serialize/unserialize regardless of how the UIImage was created by using the extension method on UIImage:
let originalImage: UIImage = ...
let cgData = image.cgImage!.png!
let image = UIImage(data: cgData)!
Upvotes: 0
Reputation: 1637
Swift 4.2
let dataPng = image.pngData() // return image as PNG. May return nil if image has no CGImageRef or invalid bitmap format
let dataJpg = image.jpegData(compressionQuality: 1) // return image as JPEG. May return nil if image has no CGImageRef or invalid bitmap format. compression is 0(most)..1(least)
Upvotes: 12
Reputation: 1028
When initialising a UIImage
object with init(data: originalData)
, that originalData
will be converted into raw data in some kind of internal format. These data can be retrieved later with
let rawData = myImage.cgImage?.dataProvider?.data as Data?
However because the rawData
is raw, it is going to be even larger than when using UIImagePNGRepresentation
.
Upvotes: 30
Reputation: 904
You can expect that a UIImage is an object formatted for display and so won't be using the original data (which is probably in PNG or JPEG format) but more likely a pixel array or some other internal format. In other words, UIImage(data: foo)
will not retain foo
.
If you just want to use it elsewhere in your program, the original UIImage will do fine (I presume that's not actually the case here)
If you want to serialise, UIImagePNGRepresentation(...)
will work but will be oversized if the original was a JPEG; UIImageJPEGRepresentation(...)
will often result in slightly oversize data and is slightly lossy if your original was PNG. It should be okay to pick one based on the way the image will be displayed and the format you expect to be provided. If you happen to be using PNG in and want PNG out, you should get a good file size and almost identical data, special PNG chunks aside.
If you want to get an exact copy of the original data (perhaps to save a file after thumbnailing, or to SHA1 it), then you need to retain it separately. You might do something like:
var image:UIImage
var imageData:NSData {
didSet {
image = UIImage(data: imageData)
}
}
Upvotes: 6
Reputation: 879
Just because I stumbled upon this and i like swift :)
Here is the swift translation of Caroiline's post.
var imageData = UIImagePNGRepresentation(image)
Or
var imageData = UIImageJPEGRepresentation(image, 0.7)
Upvotes: 8
Reputation: 809
Things have changed since the above answer was given, for those still looking because they share CipherCom's concern: iOS 5 has added the CIImage property.
Upvotes: -2