I have firstly convert the image to raw pixels and again convert the pixels back to UIImage, after converting the image it changes it color and also become some transparent, I have tried a lot but not able to get the problem. Here is my code:
-(UIImage*)markPixels:(NSMutableArray*)pixels OnImage:(UIImage*)image{ CGImageRef inImage = image.CGImage; // Create off screen bitmap context to draw the image into. Format ARGB is 4 bytes for each pixel: Alpa, Red, Green, Blue CGContextRef cgctx = [self createARGBBitmapContextFromImage:inImage]; if (cgctx == NULL) { return nil; /* error */ } size_t w = CGImageGetWidth(inImage); size_t h = CGImageGetHeight(inImage); CGRect rect = {{0,0},{w,h}}; // Draw the image to the bitmap context. Once we draw, the memory // allocated for the context for rendering will then contain the // raw image data in the specified color space. CGContextDrawImage(cgctx, rect, inImage); // Now we can get a pointer to the image data associated with the bitmap // context. int r = 3; int p = 2*r+1; unsigned char* data = CGBitmapContextGetData (cgctx); int i = 0; while (data[i]&&data[i+1]) { // NSLog(@"%d",pixels[i]); i++; } NSLog(@"%d %zd %zd",i,w,h); NSLog(@"%ld",sizeof(CGBitmapContextGetData (cgctx))); for(int i = 0; i< pixels.count-1 ; i++){ NSValue*touch1 = [pixels objectAtIndex:i]; NSValue*touch2 = [pixels objectAtIndex:i+1]; NSArray *linePoints = [self returnLinePointsBetweenPointA:[touch1 CGPointValue] pointB:[touch2 CGPointValue]]; for(NSValue *touch in linePoints){ NSLog(@"point = %@",NSStringFromCGPoint([touch CGPointValue])); CGPoint location = [touch CGPointValue]; for(int i = -r ; i<p ;i++) for(int j= -r; j<p;j++) { if(i<=0 && j<=0 && i>image.size.height && j>image.size.width) continue; NSInteger index = (location.y+i) * w*4 + (location.x+j)* 4; index = 0; data[index +3] = 125; } } } // When finished, release the context CGContextRelease(cgctx); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGDataProviderRef dp = CGDataProviderCreateWithData(NULL, data, w*h*4, NULL); CGImageRef img = CGImageCreate(w, h, 8, 32, 4*w, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big, dp, NULL, NO, kCGRenderingIntentDefault); UIImage* ret_image = [UIImage imageWithCGImage:img]; CGImageRelease(img); CGColorSpaceRelease(colorSpace); // Free image data memory for the context if (data) { free(data); } return ret_image; }
First one is original image and second image is after applying this code.


You have to ask the CGImageRef if it uses alpha or not, and the format of the components per pixel – look at all the CGImageGet… functions. Most likely the image is not ARGB but BGRA.
I often create and render pure green images then print out the first pixel to insure I got it right (BGRA -> 0 255 0 255) etc. It really gets confusing with host order etc and alpha first or last (does that mean before host order is applied before or after?)
EDIT: You told the CGDataProviderCreateWithData to use ‘kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big’, but I don’t see you asking the original image for how its configured. My guess is that changing ‘kCGBitmapByteOrder32Big’ to ‘kCGBitmapByteOrder32Little’ will fix your problem but the alpha may be wrong too.
Images can have different values for alpha and byte order so you really need to ask the original image how its configured then adapt to that (or remap the bytes in memory to whatever format you want.)