Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

siliconKai

macrumors newbie
Original poster
Sep 4, 2008
14
0
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 ...
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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.
 

siliconKai

macrumors newbie
Original poster
Sep 4, 2008
14
0
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
 

Sbrocket

macrumors 65816
Jun 3, 2007
1,250
0
/dev/null
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.
 

firewood

macrumors G3
Jul 29, 2003
8,108
1,345
Silicon Valley
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.

.
 

buckyballs

macrumors regular
Dec 22, 2006
176
97
Code:
NSData *imageData = UIImagePNGRepresentation(image);

Believe it or not, this works!
 

jinhaogxy

macrumors member
Oct 15, 2008
31
0
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
       ...

}
 

siliconKai

macrumors newbie
Original poster
Sep 4, 2008
14
0
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 ;-)
 

lovenemo

macrumors newbie
Aug 8, 2011
2
0
can you give me your own imagePicker class which can return the path of the image?

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

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.
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
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.

siliconKai hasn't been seen around these forums since June of 2009. I wouldn't hold you breath.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.