UIImage -> NSData

Discussion in 'iOS Programming' started by siliconKai, Sep 8, 2008.

  1. siliconKai macrumors newbie

    Joined:
    Sep 4, 2008
    #1
    hey guys,

    I want to get access to the information of my UIImage object which is displayed on the screen with an UIImageView object called imageView.

    Does somebody know how I get an NSData object from the UIImage?

    I found something interesting in the web, but it does not work.
    NSData* imgData = UIImageJPEGRepresentation(imageView.image, 1.0);
    The size of the NSData object is zero, although there is an image displayed in the UIImageView object.

    Thanks for your help ...
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    UIImageJPEGRepresentation() returns the data for a jpeg representation. This isn't the raw image data. You'd need to use CoreGraphics functions to get to the data.
     
  3. siliconKai thread starter macrumors newbie

    Joined:
    Sep 4, 2008
    #3
    UIImageJPEGRepresentation() too slow

    Hi guys,
    i have written an app, that sends an image on the iphone via wifi to a computer. I am using an UIImagePicker which returns me an UIImage.

    UIImageJPEGRepresentation() is very slow. it takes about 4 seconds to compress the image.

    Is there a faster solution?
    I would like to get the path to the displayed image if possible.

    Thanks for your help
     
  4. Sbrocket macrumors 65816

    Sbrocket

    Joined:
    Jun 3, 2007
    Location:
    /dev/null
    #4
    You can't get the path to actual file, no. In all likelihood there is no "actual file" if you pick a photo out of the Photo Library rather than the Image Roll since the former are stored in Apple's ithmb format. If you want an NSData representation, you'll need to use either UIImageJPEGRepresentation() or UIImagePNGRepresentation(). You could (and probably should) do this and your upload in a separate thread if its preventing drawing of something like a progress bar on your main thread.
     
  5. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #5
    You can try using Core Graphics to create a bitmap and draw your image into that bitmap, which you can then cast into a byte array for NSData.

    .
     
  6. buckyballs macrumors regular

    Joined:
    Dec 22, 2006
    #6
    Code:
    NSData *imageData = UIImagePNGRepresentation(image);
    Believe it or not, this works!
     
  7. jinhaogxy macrumors member

    jinhaogxy

    Joined:
    Oct 15, 2008
    #7
    Code:
    
    - (CGContextRef)CreateRGBBitmapContext:(CGImageRef)inImage
    {
        CGContextRef    context = NULL;
    	CGColorSpaceRef colorSpace;
        int             bitmapByteCount;
        int             bitmapBytesPerRow;
    	
    	size_t width = CGImageGetWidth(inImage);
    	size_t height = CGImageGetHeight(inImage);
    	bitmapBytesPerRow   = (width * 4);
        bitmapByteCount     = bitmapBytesPerRow * height;
    	
    	colorSpace = CGColorSpaceCreateDeviceRGB();
    
    	if(processData == nil)
    	{
    		processData = malloc(bitmapByteCount);
    		memset(processData, 0, bitmapByteCount);
    	}
    	else
    	{
    		memset(processData, 0, bitmapByteCount);
    	}
    	
        if (processData == NULL) 
        {
            return NULL;
        }
    
    	if (colorSpace == NULL)
        {
    		CGColorSpaceRelease( colorSpace );
            return NULL;
        }
    	
    	size_t bitsPerComponent = 8;
    	context = CGBitmapContextCreate (processData, 
    									 width, 
    									 height,	
    									 bitsPerComponent,
    									 bitmapBytesPerRow, 
    									 colorSpace, 
    									 kCGImageAlphaNoneSkipFirst);
    	
    	CGColorSpaceRelease( colorSpace );
    	if (context == NULL)
        {
            free (processData);
        }
        return context;
    }
    
    
    CGContextRef cgContext = [self CreateRGBBitmapContext:originalImage.CGImage];
    	if(cgContext == NULL)
    	{
    		return nil;
    	}
    	
        size_t w = CGImageGetWidth(originalImage.CGImage);
        size_t h = CGImageGetHeight(originalImage.CGImage);
        CGRect rect = {{0,0},{w,h}}; 
        CGContextDrawImage(cgContext, rect, originalImage.CGImage); 
    	void *data = CGBitmapContextGetData (cgContext);
    
           // do something with data
           ...
    
    }
    
     
  8. siliconKai thread starter macrumors newbie

    Joined:
    Sep 4, 2008
    #8
    Thanks!

    Okay thank you for your answers.
    I have already started to develop my own ImagePicker which gives me the path to the image as a return. This will be much faster, than UIImageJPEGRepresentation.
    I haven't thought it about it before, but because of sandboxing there is of course no method which returns the path to image of the camera roll.

    @buckyballs: I believe you ;-)
     
  9. slackerp macrumors newbie

    Joined:
    Jan 21, 2009
    #9
    Re: Thanks

    silicon Kai,

    How do you manage to get the path? Thanks

    -slackerp
     
  10. lovenemo macrumors newbie

    Joined:
    Aug 8, 2011
    #10
    can you give me your own imagePicker class which can return the path of the image?

    Hello,I have been confused by this question for a couple of days, can you show me your own ImagePicker class which can return the path of the image captured by camera? Or can you give me some suggestion about the same question as you listed above? I would appreciate any help.
     
  11. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #11
    siliconKai hasn't been seen around these forums since June of 2009. I wouldn't hold you breath.
     
  12. lovenemo macrumors newbie

    Joined:
    Aug 8, 2011
    #12
    it is a pity

    do you have his/her email?or other?
     
  13. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #13
    No.
     

Share This Page