Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 25, 2013, 07:46 AM   #1
xArtx
macrumors 6502a
 
Join Date: Mar 2012
Changing UIImage contents

Hi,
You're not supposed to be able to alter the content of a UIImage directly,
but I wanted to, so I loaded an image file from main bundle into an NSdata array,
and from there to a C char array, make changes there, and load back to UIImage from there.

Code:


	// Do once at startupů ie. ViewDidLoad

	unsigned char imgfile[imgbuffersize];
	int imgfilelength;

	UIImage *myImage;
	NSData *mynewData;

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"image" ofType:@"bmp"];
    NSData *myData = [NSData dataWithContentsOfFile:filePath];
    
    NSUInteger len = [myData length];
    Byte *byteData = (Byte*)malloc(len);
    memcpy(byteData, [myData bytes], len);
    
    imgfilelength = len;
    
    for (int ix = 0; ix < len; ix++) {
    imgfile[ix] = byteData[ix];
    }


endless_loop() {
	//
	//Access and change bitmap data stored the imgfile array here
	//for example, make the left half of the image grayscale
        //by finding the mean average of the RGB component of each
        //pixel you want to change, then write that value to all three
        //components of the pixel.


    mynewData = [NSData dataWithBytes:(const void *)imgfile length:sizeof(char)*imgfilelength];
    myImage = [UIImage imageWithData: mynewData];
    CGPoint imagePoint = CGPointMake(5.0,150);
    [myImage drawAtPoint:imagePoint];
}
Aside from possible overhead, which I'm not sure about since UIImage is only a
pointer if I'm not mistaken, I don't think the entire image is being copied
from one location in memory to another for each instance.

Is there anything bad happening here memory wise?
I have recently treated the C bitmap array as a memory buffer,
and have a program using it to transform the original image from the main bundle.
xArtx is offline   0 Reply With Quote
Old Jan 26, 2013, 01:39 AM   #2
xArtx
Thread Starter
macrumors 6502a
 
Join Date: Mar 2012
Interestingly enough, although iOS doesn't appear to support it,
it's possible to save a bitmap image file to the photos app with this:
Code:
       myData = [NSData dataWithBytes:(const void *)imgfile length:sizeof(char)*imgfilelength];
       myImage = [UIImage imageWithData: myData];
       UIImageWriteToSavedPhotosAlbum(myImage, nil, nil, nil);
The bmp images work perfectly in the photos app,
but when transferred to a PC, none of the programs will open them.
One of the problems is the image height is -568 instead of 568,
among other problems.
xArtx is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 12:32 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC