Saving a Quartz Context to the Camera Roll. How do I do it? :)
I made a little program to sketch with fingers, just for getting to know the sdk. How can I insert functionality to save the sketch to the camera roll?
I'd probably go with something like this (simplified, yes, but it covers what you need to do).
Code:
UIGraphicsBeginImageContext(self.view.frame.size);
/*Draw your image into your context here */
UIImageWriteToSavedPhotosAlbum(UIGraphicsGetImageFromCurrentImageContext(), nil, nil, nil);
UIGraphicsEndImageContext();
To fix your current issue though, try:
Code:
//If the image is within the root of your app.
[UIImage alloc imageNamed:@"pic.png"];
//Or, if it isn't in the root but is still within the app bundle: ie /My.app/Images
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"pic" ofType:@"png"]]];
//Or, if the image is in your Documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pathToYourImage = [NSString pathWithComponents:[NSArray arrayWithObjects:documentsDirectory, @"pic.png", nil]];
[UIImage imageWithContentsOfFile:pathToYourImage];
I don't know about the black thumbnail. The image is flipped because the CoreGraphics drawing surface' y axis is correct, and UIKit's y axis is inverted (for some weird reason). You will need to translate the grid and scale the image accordingly. Use these two lines:
Code:
CGContextTranslateCTM(UIGraphicsGetCurrentContext, 0.0, CGImageGetHeight(theImage)); //Whereas theImage is a CGImageRef. Using that is much more accurate than feeding in a height variable, but if you must, use a CGFloat variable.
CGContextScaleCTM(UIGraphicsGetCurrentContext, 1.0, -1.0);
CGContextTranslateCTM(_myContext, 1, -1);
CGContextScaleCTM(_myContext, 0, 480); //the image is the whole screen
CGImageRef temp = CGBitmapContextCreateImage(_myContext);
but this code doesn't have any effect, it gets saved just as before, mirror flipped, and what's more - i played with the translate and scaling parameters and it looks like they're not doing anything.