2

First of all sorry for my english. I'm doing paint iOS application. I was decided to use pixel by pixel processing of image. It is needed to create difficult "brush" tools. I was use this algorithm.

My code:

ViewController.h

#import <UIKit/UIKit.h>

@interface MVRViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) UIImage *image;

@end

ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];

    self.image = [UIImage imageNamed:@"grid_750x450.png"];
    self.imageView.image = self.image;
}


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    NSSet *allTouches = [event allTouches];
    UITouch *touch = [[allTouches allObjects] objectAtIndex:0];


    CGPoint imageViewPoint = [touch locationInView:self.imageView];

    if ((imageViewPoint.x < self.imageView.frame.size.width) && (self.imageViewPoint.y < imageView.frame.size.height)) {

        // Create image buffer
        CGContextRef ctx;
        CGImageRef imageRef = [self.image CGImage];
        NSUInteger width = CGImageGetWidth(imageRef);
        NSUInteger height = CGImageGetHeight(imageRef);
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        unsigned char *rawData = malloc(height * width * 4);
        NSUInteger bytesPerPixel = 4;
        NSUInteger bytesPerRow = bytesPerPixel * width;
        NSUInteger bitsPerComponent = 8;
        CGContextRef context = CGBitmapContextCreate(rawData, width, height,
                                                     bitsPerComponent, bytesPerRow, colorSpace,
                                                     kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
        CGColorSpaceRelease(colorSpace);

        CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
        CGContextRelease(context);


        // Do something with image buffer
        int X = imageViewPoint.x;
        int Y = imageViewPoint.y;

        for (int X1=X-14; ((X1<X+14) && (X1<width)); X1++) {
            for (int Y1=Y-14; ((Y1<Y+14) && (Y1<height)); Y1++) {

                int byteIndex = (bytesPerRow * Y1) + X1 * bytesPerPixel;

                rawData[byteIndex]=255; // new red
                rawData[byteIndex+1]=0; // new green
                rawData[byteIndex+2]=0; // new blue
            }
        }

        // Save image buffer to UIImage
        ctx = CGBitmapContextCreate(rawData,
                                    CGImageGetWidth( imageRef ),
                                    CGImageGetHeight( imageRef ),
                                    8,
                                    CGImageGetBytesPerRow( imageRef ),
                                    CGImageGetColorSpace( imageRef ),
                                    kCGImageAlphaPremultipliedLast );

        imageRef = CGBitmapContextCreateImage (ctx);
        UIImage* rawImage = [UIImage imageWithCGImage:imageRef];  

        CGContextRelease(ctx);  

        self.image = rawImage;
        self.imageView.image = self.image;

        CGImageRelease(imageRef);

        free(rawData);
    }
}

Application is work. But...

My problem: application was crashed by Low memory error (on device). From the Instruments (Allocation profile) I see that unsigned char *rawData = malloc(height * width * 4); do not never released and free(rawData) function do not worked, I guess. On simulator memory growth to "infinity" (3Gb...).

Where am I wrong? Thank you!

MavrinPN
  • 21
  • 1
  • 3
  • It won't answer the question, but you should have a look at the sample code named glpaint. It is not using a pixel by pixel approach on the GPU as you do (and probably shouldn't). Or even Erica Sadun samples (chapter 1, recipe 7) in iOS6 cookbook on her github. This should give you a starting point if you really want to set the pixel color yourself. – alecail Aug 10 '13 at 20:29

0 Answers0