I am trying to take a photo, overlay a graphic, then save it to the camera roll. I thought I had it figured out, but am running into two weird issues:
- If I press the Home button immediately after the "Saving photo" dialog completes, the photo does not save. If I count to ten before pushing the Home button it saves.
- When it does save, the overlay image is there but not the photo from the camera. In the Photo Albums Camera Roll row the preview image is black. If I open the photo it is all white, with my overlay image displayed on top.
Below is the code I am using, with comments for what I thought each line was doing.
Code:
#pragma mark -
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
// create a new bitmap image context with width of 1200 x 1600 to match camera output size
UIGraphicsBeginImageContext(CGSizeMake(1200, 1600));
// get context
CGContextRef context = UIGraphicsGetCurrentContext();
// push context to make it current (need to do this manually because we are not drawing in a UIView)
UIGraphicsPushContext(context);
// drawing code comes here- look at CGContext reference for available operations
// this example draws the overlayImage into the context
UIImage *overlayImage = [UIImage imageNamed:@"overlay-image.png"];
// x position, y position, height, width
[overlayImage drawInRect:CGRectMake(160, 250, 160, 240)];
// pop context Removes the current graphics context from the top of the stack, restoring the previous context, balance calls to the UIGraphicsPushContext function
UIGraphicsPopContext();
// get a UIImage from the image context
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
// clean up drawing environment
UIGraphicsEndImageContext();
// write the combined image to the photo album
UIImageWriteToSavedPhotosAlbum(outputImage, nil, nil, nil);
[self dismissModalViewControllerAnimated:YES];
}
Any help would be greatly appreciated. I've searched, checked out sample code, tried several different ways to implement, but am really hung up on this. Thanks.