caching UIImage

Discussion in 'iOS Programming' started by daproject85, Nov 4, 2012.

  1. macrumors member

    Joined:
    Apr 13, 2011
    #1
    Hi Forum,

    I currently have a NSData object from a URL. The code is as follows:

    Code:
    NSData *data = [[NSData alloc] initWithContentsOfURL: ImageURL];
    now I would like to cache this image file and for that I would have to get the path via NSFileManager...etc etc..Then eventually use the method

    Code:
    - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag
    from NSData class

    now this image is a JPEG...so what is the difference between the following.

    Code:
    NSData *data = [[NSData alloc] initWithContentsOfURL: ImageURL];
    [data writeToFile: uniquePath atomically: YES];
    and

    Code:
    NSData *data = [[NSData alloc] initWithContentsOfURL: ImageURL];
    UIImage *image = [[UIImage alloc] initWithData: data];
    [UIImageJPEGRepresentation(image, 100) writeToFile: uniquePath atomically: YES];
    
    is there a difference between whats in the NSDATA object "data" and the NSData object that the function from UIKIT: NSData * UIImageJPEGRepresentation (
    UIImage *image,
    CGFloat compressionQuality
    );
    returns??
     
  2. macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #2
    Use NSData's isEqualToData: to test what you read from the url and what gets returned from the UIImageJPEGRepresentation function.
     
  3. thread starter macrumors member

    Joined:
    Apr 13, 2011
    #3
    yes they are different...but which one is correct?
     
  4. macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
  5. thread starter macrumors member

    Joined:
    Apr 13, 2011
    #5
    like writing them back to NSdata and making a UIImage out of them?
     
  6. macrumors 68000

    Reason077

    Joined:
    Aug 14, 2007
    #6
    Do you want to make sure to save/cache the image exactly as it appears on the web? If so, use the first one.

    If you want to convert everything to JPG (which may involve re-compressing the image, which can degrade quality), then use the second one.

    I'm guessing you probably want the first.
     
  7. macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #7
    You could use the AsyncImageView from NickLockWood which does all of this for you + it's async.. ;)
     
  8. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
  9. thread starter macrumors member

    Joined:
    Apr 13, 2011
    #9

    Reasons thanks for you reply... i noticed the following: the first method creates a file which is 767KB (same size at the jpeg file ). the second method creates a file which is about 2.7 MB. The original file is a JPEG. So now it leaves me confused as to why the second method creates a files thats bigger. But yes to answer your question I want the file exactly how it appeared in my UIImageView

    ----------

    Dejo, what is the significance of using an asyn loading/caching? Basically im asking whats so special about doing it "asynchronously" vs synchronously and what would an example of synchronous caching be?
     
  10. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    The async only applies to the loading and not the caching. Doesn't really make sense it that context. Anyways, asynchronous loading means that the main thread (which is where all UI is handled) is not blocked and therefore the image can be loaded "in the background". Therefore, the UX is never hung-up waiting for an image to load for a remote source.
     
  11. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #11
    It's a well known property of jpeg image files that if you compress them repeatedly each time the image data will change. Also, if you have a jpeg that's been saved at say 40% and then you take that image and save it at 100% the new file will be larger although it will probably look the same as the 40% version.

    There's no reason to re-compress a jpeg to a higher % compression. And if you want it to remain the same then don't change it at all.
     
  12. thread starter macrumors member

    Joined:
    Apr 13, 2011
    #12
    that made perfect sense thanks!! so the UIImageJPEGRepresentation is for more of a granular operation.
     

Share This Page