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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.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 08-07-2010, 11:58 PM   #1 (permalink)
jcc
Registered Member
 
Join Date: Aug 2010
Posts: 53
jcc is on a distinguished road
Default Screenshot to NSMutableArray

Hello,

I am attempting to create a screenshot and save it to the Documents Folder so that I can load it in an NSMutableArray. I am able to capture the screenshot and save to the documents folder with the following code:


Code:
	UIGraphicsBeginImageContext(self.view.bounds.size);
	
	[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
	
	UIImage *viewImage =UIGraphicsGetImageFromCurrentImageContext();
	
	UIGraphicsEndImageContext();
	
		
	
	
	NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(viewImage)];
	
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	
	NSString *documentsDirectory = [paths objectAtIndex:0];
	
	documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"Screenshot.png"];

	[imageData writeToFile:documentsDirectory atomically:YES];
My problem is that I don't know how to retrieve this image and load it into an NSMutableArray. I have tried a variety of ways but can't figure it out.

Any ideas?

Thanks!
jcc is offline   Reply With Quote
Old 08-08-2010, 12:37 AM   #2 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 386
SoulRed12 is on a distinguished road
Default

Use imageWithContentsOfFile to load the image, and for your path, specify the image name appended to the path for the documents directory.
SoulRed12 is offline   Reply With Quote
Old 08-08-2010, 12:48 AM   #3 (permalink)
jcc
Registered Member
 
Join Date: Aug 2010
Posts: 53
jcc is on a distinguished road
Default

So something like this?


NSString *filePath = [documentsDirectory stringByAppendingPathComponent: @"/Screenshot.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: [UIImage imageWithContentsOfFile: filePath]];

[myArray addObject:imageView];



I keep trying this but it crashes the App

Last edited by jcc; 08-08-2010 at 01:10 AM.
jcc is offline   Reply With Quote
Old 08-08-2010, 08:58 AM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by jcc View Post
So something like this?


NSString *filePath = [documentsDirectory stringByAppendingPathComponent: @"/Screenshot.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: [UIImage imageWithContentsOfFile: filePath]];

[myArray addObject:imageView];



I keep trying this but it crashes the App
Make sure documentsDirectory has been set up correctly. Many people create a function getDocumentsDirectory() that does the 3 lines of code to get the documents directory, and call that when they need it.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 08-08-2010, 12:14 PM   #5 (permalink)
jcc
Registered Member
 
Join Date: Aug 2010
Posts: 53
jcc is on a distinguished road
Default

Thanks for the help. I cannot find any examples using getDocumentsDirectory(). I know that the image is being saved into the Applications Documents Folder. I just am having trouble accessing the image to insert into an array.

I have tried other methods (such as NSFileManager to delete the file) to see if I am calling the file correctly and it all works.

I have posted the code below:

Code:
- (IBAction) saveImage:(id)sender;
{
	
	
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Screenshot Saved" message:@"A screenshot of your work has been saved to your Photos album." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alert show];
    [alert release];
	
	
	
	UIGraphicsBeginImageContext(self.view.bounds.size);
	
	[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
	
	UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
	
	UIGraphicsEndImageContext();
	
		
	

	NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(viewImage)];
	
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	
	NSString *documentsDirectory = [paths objectAtIndex:0];
	
	documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"Screenshot.png"];

	[imageData writeToFile:documentsDirectory atomically:YES];
	
	

	NSString *documentsDirectoryPath = [paths objectAtIndex:0];
	NSString *databaseFile = [documentsDirectoryPath stringByAppendingPathComponent:@"Screenshot.png"];
	
	

	[myArray addObject:databaseFile];

		
}
jcc is offline   Reply With Quote
Old 08-08-2010, 09:52 PM   #6 (permalink)
jcc
Registered Member
 
Join Date: Aug 2010
Posts: 53
jcc is on a distinguished road
Default

Any code examples that might help?

I'm completely new to all of this and don't really understand the developer lingo. However, If someone can provide a code sample that would lead me in the right direction that would be very appreciated!
jcc is offline   Reply With Quote
Old 08-09-2010, 12:22 AM   #7 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 386
SoulRed12 is on a distinguished road
Default

Wait a second--you have the code to access the documents directory in your original code:

Code:
//(from your code in the first post)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	
	NSString *documentsDirectory = [paths objectAtIndex:0];
	
	documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"Screenshot.png"];
^This is what you need. Do this again when you need to load it, and afterward just use [UIImage imageWithContentsOfFile:documentsDirectory].
SoulRed12 is offline   Reply With Quote
Old 08-09-2010, 12:44 AM   #8 (permalink)
jcc
Registered Member
 
Join Date: Aug 2010
Posts: 53
jcc is on a distinguished road
Default

Got it! Maybe I wasn't so lost after all.
jcc is offline   Reply With Quote
Reply

Bookmarks

Tags
nsdocumentdirectory, nsmutablearray, writetofile

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: 303
8 members and 295 guests
arash5500, HemiMG, linkmx, mediaspree, Objective Zero, Paul Slocum, stanny, Touchmint
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,655
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, pungs
Powered by vBadvanced CMPS v3.1.0

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