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 02-17-2011, 04:40 PM   #1 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 24
ChrisW is on a distinguished road
Question Taking and saving a screenshot

I'm trying to save off a picture of the mapView...
Code:
UIGraphicsBeginImageContext(mapView.frame.size);
[self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *mapImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSData *mapData = [NSData dataWithData:UIImagePNGRepresentation(mapImage)];
[mapData writeToFile:[self myFilePath:@"MyMap.png"] atomically:YES];
Then I later try and overwrite a button image with MyMap.png...
Code:
UIImage *image = [UIImage imageNamed:@"MyMap.png"];
[btnMap setImage:image forState:UIControlStateNormal];
I'm not sure why this isn't working. If anyone can help me out it would be greatly appreciated.
Thanks,
Chris
ChrisW is offline   Reply With Quote
Old 02-17-2011, 04:49 PM   #2 (permalink)
Indie Developer
 
iSDK's Avatar
 
Join Date: Jul 2010
Posts: 1,346
iSDK is on a distinguished road
Send a message via AIM to iSDK
Default

Firstly the UIGraphicsGetImageFromCurrentImageContext() needs to be retained if I remember correctly and secondly if I am correct I think that you are trying to take a picture of the current window 'self.window.layer'. Try mapView.layer.



Quote:
Originally Posted by ChrisW View Post
I'm trying to save off a picture of the mapView...
Code:
UIGraphicsBeginImageContext(mapView.frame.size);
[self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *mapImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSData *mapData = [NSData dataWithData:UIImagePNGRepresentation(mapImage)];
[mapData writeToFile:[self myFilePath:@"MyMap.png"] atomically:YES];
Then I later try and overwrite a button image with MyMap.png...
Code:
UIImage *image = [UIImage imageNamed:@"MyMap.png"];
[btnMap setImage:image forState:UIControlStateNormal];
I'm not sure why this isn't working. If anyone can help me out it would be greatly appreciated.
Thanks,
Chris
iSDK is offline   Reply With Quote
Old 02-17-2011, 05:13 PM   #3 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 24
ChrisW is on a distinguished road
Default

Thanks for the quick response!

Using the mapView layer makes more sense, so I fixed that part. Isn't the UIGraphicsGetImageFromCurrentImageContext() retained in the mapImage?

Here's the adjusted code...
Code:
UIGraphicsBeginImageContext(mapView.frame.size);
[[mapView layer] renderInContext:UIGraphicsGetCurrentContext()]; //also tried mapView.layer
UIImage *mapImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSData *mapData = UIImagePNGRepresentation(mapImage);
[mapData writeToFile:[self myFilePath:@"MyMap.png"] atomically:YES];

Edit:
Wanted to add that XCode says that renderInContext: method cannot be found.

Last edited by ChrisW; 02-17-2011 at 05:17 PM.
ChrisW is offline   Reply With Quote
Old 02-17-2011, 05:30 PM   #4 (permalink)
Indie Developer
 
iSDK's Avatar
 
Join Date: Jul 2010
Posts: 1,346
iSDK is on a distinguished road
Send a message via AIM to iSDK
Default

You need to import QuartzCore. Do you want to NSLog your 'myFilePath' to check that you are placing and retrieving the image from the correct place?

Quote:
Originally Posted by ChrisW View Post
Thanks for the quick response!

Using the mapView layer makes more sense, so I fixed that part. Isn't the UIGraphicsGetImageFromCurrentImageContext() retained in the mapImage?

Here's the adjusted code...
Code:
UIGraphicsBeginImageContext(mapView.frame.size);
[[mapView layer] renderInContext:UIGraphicsGetCurrentContext()]; //also tried mapView.layer
UIImage *mapImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSData *mapData = UIImagePNGRepresentation(mapImage);
[mapData writeToFile:[self myFilePath:@"MyMap.png"] atomically:YES];

Edit:
Wanted to add that XCode says that renderInContext: method cannot be found.
iSDK is offline   Reply With Quote
Reply

Bookmarks

Tags
image, mkmapview, render, save, screenshot

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: 371
13 members and 358 guests
cpsclicker, dre, Error404, Gaz, gmarro, jeroenkeij, Kirkout, Music Man, PavelMik, teebee74, whitey99, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,666
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, cpsclicker
Powered by vBadvanced CMPS v3.1.0

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