¿Cómo puedo manejar UIImage usando un puntero?

Estoy estudiando programación iOS.

i have to handling an image.

i must make an image to BMP file. but iOS doesn't support to make BMP file.

so i think that maybe i use a pointer i can make BMP file.

i make a header for BMP. and put it in an NSData objeto.

now i left saving image data's to that NSData.(i'll say bmpData)

here's start my wondering.

i have to have image which size is 128 x 64.

yo consigo el UIImage from a context. (i'll say image1)

and i make a CGSize which is 128 x 64.

i make a context2 using that size, drawing an image1

now i get the UIImage from a context2, which size is 128 x 64, resized image1.

and i make an UIImageView and use image2, it works fine. good. image2 is made well.

so i declared a pointer, which is unsigned char *.

unsigned char *bmpDataPointer = (unsigned char *)image2;

and i use for loop

for(int i = 0; i < 64; ++i)
    for(int j = 0 ; j < 128; ++j)
       // dataObj and bmpData are different NSData object
       // dataObj just contain bitmap data to check my pointer works fine or not.
       [dataObj appendBytes:&(bitmapDataPointer[i*1 + j]) length:sizeof(char)];

and i make a UIImage to check that data is valid, it fails.

UIImage *createdImageUsedByaPointer = [UIImage imageWithData:dataObj];
if(createdImageUsedByaPointer == nil)

ok run. then string nil will be presented.

why is that? i make context size 128 x 64, so i loop 128 x 64 times.

but it works bad.

how can i fix that??

how can i handling an UIImage to use a pointer??

anybody knows about that please help me.

preguntado el 04 de julio de 12 a las 05:07

Cual es el tipo de image2? -

No puedes simplemente lanzar el UIImage oponerse a unsigned char *. Because image objects are immutable, they also do not provide direct access to their underlying image data. However, you can get an NSData object containing either a PNG or JPEG representation of the image data using the UIImagePNGRepresentation and UIImageJPEGRepresentation functions. -

2 Respuestas

Don't know why you need .bmp format but ImageMagick will handle export.

Use this lib ImageMagick for iOS y el código a continuación.

UIImage *img9 = [UIImage imageNamed:@"00009_face.jpg"];
MagickWand *bmpWand = NewMagickWand();
NSData *bmpObj = UIImagePNGRepresentation(img9);
MagickSetFormat(bmpWand, "bmp");
MagickReadImageBlob(bmpWand, [bmpObj bytes], [bmpObj length]);

size_t bmp_size;
unsigned char * bmp_image = MagickGetImagesBlob(bmpWand, &bmp_size);
NSData *bmpData = [[[NSData alloc] initWithBytes:bmp_image length:bmp_size] autorelease];

NSArray *pathsArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentDirectory = [pathsArray objectAtIndex:0];
NSString * bmpPath = [documentDirectory stringByAppendingPathComponent:@"test2.bmp"];
[bmpData writeToFile:bmpPath atomically:NO];

NSLog(@"bmpPath %@", bmpPath);

Respondido 04 Jul 12, 06:07

Why are you going in that much complexity?

You can create bitmap context as follows:

CGContextRef context = CGBitmapContextCreate(data, width, height, 
            bytesPerRow, colorSpace, 
            kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

CGImageRef imageRef = CGBitmapContextCreateImage(context);
UIImage *result = [UIImage imageWithCGImage:imageRef];

here data is ur image's NSData

Respondido 04 Jul 12, 06:07

CGBitmapContextCreate(data, ...) that data type is void *. can i use follow? NSData *jpegData = UIImageJPEGRepresentation(resizeImg,0.7); and pass jpegData to data? then can i get the bitmap file? - lunasoo

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.