saving images

  • hi.

    i just started a new ios project where i want to download images using NSimage's initwithcontentsofURL: method.
    now i also want to write those images to permanent storage, but how do i do that? as far as i can tell, NSImage has no method for writing images to disk. is there something i missed? should i download them into an NSData object and save that? but if i do that, will initwithcontentsoffile: read them properly? questions, questions, questions...

    any help will be appreciated. thanks.
  • On Jul 21, 2012, at 10:03 AM, H. Miersch wrote:

    > i just started a new ios project where i want to download images using NSimage's initwithcontentsofURL: method.
    > now i also want to write those images to permanent storage, but how do i do that?

    You could always download the data given the URL using NSData, and use the NSData to initialize the image. Then you can just save the NSData to storage.

    Nick Zitzmann
    <http://www.chronosnet.com/>
  • On 21. Jul 2012, at 17:37, Nick Zitzmann wrote:

    >
    > On Jul 21, 2012, at 10:03 AM, H. Miersch wrote:
    >
    >> i just started a new ios project where i want to download images using NSimage's initwithcontentsofURL: method.
    >> now i also want to write those images to permanent storage, but how do i do that?
    >
    > You could always download the data given the URL using NSData, and use the NSData to initialize the image. Then you can just save the NSData to storage.

    thought so. and when i load it again, i'd have to load it into an NSData object and create the image from that, correct?
  • NSImage is for OS X. You need to use UIImage on iOS. One difference is that UIImage doesn't have an -initWithContentsOfURL: method.

    Note that UIImage conforms to NSCoding, so once you've loaded a UIImage through whichever means you prefer (NSData, the URL loading system, etc.) you can archive the image to an NSData (and of course unarchive) in the usual manner.

    (Sent from my iPhone.)

    --
    Conrad Shultz

    On Jul 21, 2012, at 9:03, "H. Miersch" <hmiersch...> wrote:

    > hi.
    >
    > i just started a new ios project where i want to download images using NSimage's initwithcontentsofURL: method.
    > now i also want to write those images to permanent storage, but how do i do that? as far as i can tell, NSImage has no method for writing images to disk. is there something i missed? should i download them into an NSData object and save that? but if i do that, will initwithcontentsoffile: read them properly? questions, questions, questions...
    >
    > any help will be appreciated. thanks.
  • On Jul 21, 2012, at 10:41 AM, H. Miersch wrote:

    >
    > On 21. Jul 2012, at 17:37, Nick Zitzmann wrote:
    >
    >>
    >> On Jul 21, 2012, at 10:03 AM, H. Miersch wrote:
    >>
    >>> i just started a new ios project where i want to download images using NSimage's initwithcontentsofURL: method.
    >>> now i also want to write those images to permanent storage, but how do i do that?
    >>
    >> You could always download the data given the URL using NSData, and use the NSData to initialize the image. Then you can just save the NSData to storage.
    >
    > thought so. and when i load it again, i'd have to load it into an NSData object and create the image from that, correct?

    Well, once it's on disk, you can use -[NSImage initWithContentsOfURL:] with the file URL and you won't have to make an NSData object if you want to skip that step.

    Nick Zitzmann
    <http://www.chronosnet.com/>
  • On 22/07/2012, at 2:58 AM, Conrad Shultz wrote:

    > Note that UIImage conforms to NSCoding, so once you've loaded a UIImage through whichever means you prefer (NSData, the URL loading system, etc.) you can archive the image to an NSData (and of course unarchive) in the usual manner.

    UIImage seems somewhat different, but on the Mac (NSImage) this is a really bad idea.

    iOS has some utility functions such as UIIMageJPEGRepresentation() which converts a UIImage to data in JPEFG format, and there are others for PNG, etc. Be nice if NSImage had these as well, though there are more long-winded ways to achieve same.

    If you can track and save the original NSData for the image, that's optimal, but if you can't, one of these functions should sort you out.

    --Graham
  • On Sat, 21 Jul 2012 17:03:23 +0100, "H. Miersch" <hmiersch...> said:
    >
    > i just started a new ios project where i want to download images using NSimage's initwithcontentsofURL: method.
    > now i also want to write those images to permanent storage, but how do i do that?

    (1) There is no NSImage on iOS.

    (2) Do not neglect the Image I/O framework:

    http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conce
    ptual/ImageIOGuide/imageio_intro/ikpg_intro.html


    Some sample code (from my book) here:

    https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/ch36p
    912imageIO/p747p766imageIO/ViewController.m


    m.

    --
    matt neuburg, phd = <matt...>, <http://www.apeth.net/matt/>
    A fool + a tool + an autorelease pool = cool!
    Programming iOS 5! http://shop.oreilly.com/product/0636920023562.do
previous month july 2012 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today