简体   繁体   中英

how can i handling UIImage using a pointer?

i'm studying iOS programming.

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 object.

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.

i get the 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)
{
  NSLog(@"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.

Why are you going in that much complexity?

You can create bitmap context as follows:

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


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

here data is ur image's NSData

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

Use this lib ImageMagick for iOS and the code below.

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];
free(bmp_image);
DestroyMagickWand(bmpWand);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM