Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-19-2008, 07:14 AM   #1 (permalink)
New Member
 
Join Date: Nov 2008
Posts: 57
Default 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?

Thanks.
kzad is offline   Reply With Quote
Old 11-19-2008, 10:37 AM   #2 (permalink)
Registered Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,122
Default

Do a search here on 'UIImageWriteToSavedPhotosAlbum' and you will find code that does what you need.
RickMaddy is offline   Reply With Quote
Old 11-19-2008, 10:59 AM   #3 (permalink)
New Member
 
Join Date: Nov 2008
Posts: 57
Default

Great, this is exactly what I needed. For anyone else viewing this post, if you draw using quartz you can create a UIImage from a CGImageRef with

- (id)initWithCGImage: (CGImageRef)CGImage
kzad is offline   Reply With Quote
Old 11-19-2008, 11:18 AM   #4 (permalink)
New Member
 
Join Date: Nov 2008
Posts: 57
Default

hmm, is there any problem with calling this function in the iphone simulator?

probably i'm doing something wrong though, when i try to call the funciton like this:

UIImage *toSave = [[UIImage alloc] initWithContentsOfFile:@"pic.png"];
UIImageWriteToSavedPhotosAlbum(toSave, nil, nil, nil);

my program crashed with the following stack:



any suggestions?

Last edited by kzad; 11-19-2008 at 11:25 AM.
kzad is offline   Reply With Quote
Old 11-19-2008, 06:26 PM   #5 (permalink)
New Member
 
Join Date: Nov 2008
Posts: 74
Default

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];
SkylarEC is offline   Reply With Quote
Old 11-21-2008, 05:59 AM   #6 (permalink)
New Member
 
Join Date: Nov 2008
Posts: 57
Default

Thanks a lot for your help. For various reasons I couldn't implement it exactly like you suggested, what I did do in the end was something like this:

toSave = [[UIImage alloc] initWithCGImage:
CGBitmapContextCreateImage(_myContext);];

Later I save it to the album.

This works but for two problems, which you might be able to help me with:

1. The images are saved "mirrored" on the horizontal axis. It's pretty weird, like 'v' would become '^'.

2. The thumbnails in the photoalbum are solid black, no matter what I draw.

Any suggestions?
kzad is offline   Reply With Quote
Old 11-23-2008, 05:42 AM   #7 (permalink)
New Member
 
Join Date: Nov 2008
Posts: 74
Default

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);
SkylarEC is offline   Reply With Quote
Old 11-27-2008, 01:22 PM   #8 (permalink)
New Member
 
Join Date: Nov 2008
Posts: 57
Default not working

I'm running the following code when saving:

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.

i'm at a loss. help would be greatly appreciated.
kzad is offline   Reply With Quote
Old 11-28-2008, 06:22 AM   #9 (permalink)
New Member
 
Join Date: Nov 2008
Posts: 57
Default Solution

I finally solved everything like this:

UIGraphicsBeginImageContext(self.window.bounds.siz e);
[self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
toSave = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

takes care of the inversion problem and the thumbnails without a need for scaling.
kzad is offline   Reply With Quote
Reply

Bookmarks

Tags
camera, quartz, roll

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 274
20 members and 254 guests
ADY, AragornSG, Bertrand21, Dani77, Dattee, Duncan C, fkmtc, HDshot, HemiMG, iDifferent, JasonR, macquitzon216, mer10, prchn4christ, Rudy, sacha1996, sneaky, spiderguy84, Sunny46, theone8one
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,231
Posts: 380,768
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:11 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0