I have a UIImage that I want to write to disk as PNG. The thing is that for some reason I don't want to use the UIImagePNGRepresentation . That's because I want to do stuff with the original CGImage embedded in the UIImage before I make the save.
Here is my code so far (provided I have a test png image that I load to perform my tests)
for convenience reasons, here is the code I use to obtain the path of the documents directory:
Although this code has a result, the result is an unreadable "image.png" file that when opened with "Preview" i get an error that is is corrupted.
I am grateful for any ideas or recommendations.
Here is my code so far (provided I have a test png image that I load to perform my tests)
Code:
UIImage *image = [UIImage imageNamed:@"cameraicon.png"];
//self.imageView.image = image;
CGDataProviderRef dataProvider = CGImageGetDataProvider(image.CGImage);
CFDataRef data = CGDataProviderCopyData(dataProvider);
CGDataProviderRelease(dataProvider);
NSString *imagePathLocation = [[self documentsDirectory]stringByAppendingString:@"/image.png"];
const char *fileNameC = [imagePathLocation cStringUsingEncoding:NSUTF8StringEncoding];
const UInt8 *buffer = CFDataGetBytePtr(data);
SInt32 bufferLength = CFDataGetLength(data);
FILE *file = fopen(fileNameC, "wb");
if (!file) {
NSLog(@"error opening file!");
}
NSLog(@"trying to write %i bytes to: %@",bufferLength, imagePathLocation);
fwrite(buffer, 1, bufferLength, file);
for convenience reasons, here is the code I use to obtain the path of the documents directory:
Code:
- (NSString *)documentsDirectory
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
return documentsDirectoryPath;
}
Although this code has a result, the result is an unreadable "image.png" file that when opened with "Preview" i get an error that is is corrupted.
I am grateful for any ideas or recommendations.