111

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];

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
eugene
  • 39,839
  • 68
  • 255
  • 489

7 Answers7

191
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.

Caroline
  • 4,875
  • 2
  • 31
  • 47
  • 98
    I don't know why this answer is up-voted so many times nor that it is accepted. The OP asked to retrieve the data that was given initially. Using `UIImageJPEGRepresentation` or `UIImagePNGRepresentation` **will** alter the data and do a reconversion. Is there any way to really achieve what was asked for? – Patrik Mar 12 '13 at 18:19
  • 6
    This answer is no good, because it will lower the quality of the image if it is already in a lossy format. – 1dayitwillmake Jul 17 '13 at 17:24
  • 2
    @CipherCom I see what you're saying about wanting the exact data back. However, I think for the majority of people that come across question, this answer will be perfect for their needs... hence the upvotes. If you come across an answer for getting the original data back, please share ;) – tybro0103 Mar 06 '14 at 16:16
  • 1
    @1dayitwillmake Even the PNG rep? Not my area of expertise, but I thought PNGs don't suffer loss? – tybro0103 Mar 06 '14 at 16:17
  • @Bob Spryn This won't help. The property CIImage is only set if it was initialized with imageWithCIImage:. Also this isn't directly the used data but rather another image representation object. – King-Wizard Mar 18 '15 at 09:46
  • So am I the only one who doesn't understand this? I mean, what is this? Is this an HTML property, or a javascript snippet, c# code, etc.? Could someone please explain this a little more thoroughly? (There seems to be a lot of upvotes) But, what are the dependencies, and where do you put this code? – Penjimon Jan 29 '18 at 19:40
  • @Penjimon - this is Objective C – Caroline Jan 30 '18 at 23:32
  • Thank you for letting me know! I actually had no idea. Anyone know how to do it with C#? – Penjimon Jan 31 '18 at 08:56
28

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.

yesleon
  • 928
  • 11
  • 14
11

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)
maslovsa
  • 1,589
  • 1
  • 14
  • 15
7

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) 
nsij22
  • 869
  • 10
  • 20
6

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.

  1. 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)

  2. 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.

  3. 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)
        }
    }
    
Jim Driscoll
  • 894
  • 11
  • 8
0

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)!
Senseful
  • 86,719
  • 67
  • 308
  • 465
-2

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.

Rafe
  • 753
  • 4
  • 20
  • I'm not seeing a way to use CIImage to get the underlying NSData... am I missing something? – tybro0103 Mar 06 '14 at 16:30
  • 4
    This won't help. The property ``CIImage`` is only set if it was initialized with ``imageWithCIImage:``. Also this isn't directly the used data but rather another image representation object. – Patrik Apr 03 '14 at 08:11