View Full Version : UIImage -> NSData
siliconKai
Sep 8, 2008, 09:42 AM
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
Sep 8, 2008, 02:48 PM
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
Dec 11, 2008, 07:05 AM
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
Dec 12, 2008, 05:28 AM
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
Dec 12, 2008, 08:58 AM
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
Dec 12, 2008, 01:09 PM
NSData *imageData = UIImagePNGRepresentation(image);
Believe it or not, this works!
jinhaogxy
Dec 14, 2008, 10:47 PM
- (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
Dec 15, 2008, 04:24 AM
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 ;-)
slackerp
Jan 21, 2009, 07:24 PM
silicon Kai,
How do you manage to get the path? Thanks
-slackerp
lovenemo
Aug 8, 2011, 03:11 AM
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
Aug 8, 2011, 03:50 AM
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.
lovenemo
Aug 8, 2011, 08:07 AM
siliconKai hasn't been seen around these forums since June of 2009. I wouldn't hold you breath.
do you have his/her email?or other?
jiminaus
Aug 8, 2011, 08:59 AM
do you have his/her email?or other?
No.
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.