I am developing an iPhone app for creating images using built in graphics and user defined text.
I want to be able to have my app, with built in graphics and user defined text, which can then be sent as a single image (much like a screenshot) to the email app to be emailed.
Is there a way to do this without taking a screenshot, leaving the app, going into the Photos app, selecting the screenshot, and emailing it from there?
Ultimately I would like to be able to have a button in my app that the user could tap, and the whole screen would be captured and sent directly to the mail app.
Any pointers gratefully accepted!
To expand upon Brent's answer, the following code will grab a screenshot and save it out to the Documents directory as a PNG called screenshot.png:
UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow];
UIGraphicsBeginImageContext(screenWindow.frame.size);
[screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *screenshotPNG = UIImagePNGRepresentation(screenshot);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSError *error = nil;
[screenshotPNG writeToFile:[documentsDirectory stringByAppendingPathComponent:#"screenshot.png"] options:NSAtomicWrite error:&error];
This is a little crude, as it will leave a blank spot near the top of the screen for the title bar, and doesn't appear to grab the content from CAEAGLLayers.
Also, I don't believe you can use the standard mailto:// URL construction, followed by openURL, to send MIME-encoded attachments. Maybe the 3.0 SDK fixes this, but I've yet to play with it. You may need to use something like sksmtpmessage to send the message directly from within your application.
There's also the private API UIGetScreenImage. It is used like so:
CGImageRef UIGetScreenImage();
#interface UIImage (ScreenImage)
+ (UIImage *)imageWithScreenContents;
#end
#implementation UIImage (ScreenImage)
+ (UIImage *)imageWithScreenContents
{
CGImageRef cgScreen = UIGetScreenImage();
if (cgScreen) {
UIImage *result = [UIImage imageWithCGImage:cgScreen];
CGImageRelease(cgScreen);
return result;
}
return nil;
}
#end
This function may be combined with UIImagePNGRepresentation to produce a PNG.
Apple is now allowing applications to use the;
CGImageRef UIGetScreenImage(void);
function. Although you must remember that it returns a retained CGImageRef and you'll have to manage your memory accordingly.
They also say that, "...a future release of iPhone OS may provide a public API equivalent of this functionality. At such time, all applications using UIGetScreenImage() will be required to adopt the public API."
skpsmtpmessage is great. If fact, it's so good, I went and cloned it and added sample project on github. The sample GUI below adds a few bells and whistles, like a progress bar and some other goodies, but it basically maintains the core skpsmtpmessage code.
http://github.com/kailoa/iphone-smtp/tree/master
I have no idea if this will work, but you can try creating a bitmap-backed graphics context, then getting your root view's Core Animation layer and calling its -renderInContext: method. That might do it, but I've never tried it.
Perhaps, though, you should consider a different approach. Is it just that you've written a bunch of custom drawing code that's visible on screen and you want to be able to draw into a file or memory buffer too? If so, perhaps you should factor that drawing code out of your view and into a separate object that your view simply uses. That would allow you to very easily draw it both ways.
In OS 3.0, you can use MFMailComposeViewController:
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker addAttachmentData:screenshotPNG mimeType:#"image/png" fileName:#"PNGfromMyApp"];
Related
I have a UIWebView where I display dynamically generated HTML via
[_webView loadHTMLString:htmlString baseURL:applicationDocumentsDirectory];
The HTML contains images such as <img src="chart1.png">. Each time the HTML is generated, the images are also freshly generated. (I have checked that they are indeed in the right location and updated.) However, after the first run, if I change the data and relaunch my UIWebView, it uses the old images.
I have tried:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
to no avail. (Not surprising, maybe, as this relates to NSURLRequests.)
I know that there is the possibility of absolute URLs, but I have not tried this at it seems cumbersome and I want it to work with these simple relative URLs. The issue is not finding the images but updating them appropriately.
I also know that you could invent some scheme tricking the browser into thinking that the image is dynamic by changing the src to something like chart1.png?1234 where the number is random generated and always unique. This also seems like a useless workaround for an issue that should be simple to solve.
Any ideas?
You should have a proper reference to the image, otherwise it may not work. If you have the image and the html file in the same level then try using something like this,
<img src="./chart1.png">
Or if you want the images inside the folder then refer it using the proper path reference to it.
I hope it works.
hi i was having a similar problem with uiwebview whenever i loaded a previously loaded page old content was displayed. after investigating further i found that uiwebview content is loaded in the background and then displayed after its finished loading; however while it is loading old content is displayed. I fixed my problem by making my own uiwebview loading delegates and adding my own loading symbol insteaad of displaying old content.
-(void)webViewDidStartLoad:(UIWebView *)webView{
webView.hidden = true;
loader.animationImages =[NSArray arrayWithObjects:
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"4.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"5.png"]
,nil];
loader.animationDuration = 1.5;
loader.animationRepeatCount = 100;
[loader startAnimating];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
[loader stopAnimating];
webView.hidden = false;
}
remember to link the delegates and all that other stuff
i hope this helps :)
You need to provide the absolute path of the image source.
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
#"/chart1.png" ];
I hope this works
I'm just a beginner to iphone development.
Recently i'm doing a project in which images need to be swiped. Could any one help me with it?
The images are stored in a server whose link is given.
I'm need it badly. So please help me
thank you
There are two ways.
You could either look at using a Paging Scroll View
Or, You could look at getting a series of UIImageViews with the correct images in each and then setting up a UISwipeGestureRecogniser for both directions. On the swipe's event handler you can adjust the x and y positions of the imageViews.
This is basic stuff. You should read some of the How-to's on Apple's developer website to help you with the base knowledge.
Edit:
Regarding the Internet images, the code can be adapted into the paging scroll view example:
NSString *path = #"http://merrimusings.mu.nu/archives/images/groundhog2.jpg";
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data cache:NO];
Load the strings from an NSMutableArray instead and you're good to go!
Use Three20 photo Album. Your problem will be solved.
I am using the following (simple) code to load a PDF from the documents folder in my app into a UIWebView. The performance is very poor. I tried loading the same PDF from the web via Safari and the performance was great. Does anyone have any ideas? (this viewController is being presented as a modalViewController).
- firstView.m
InfoViewController *mcontroller = [[InfoViewController alloc] init];
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *pathToPDF = [NSString stringWithFormat:#"%#/myPDF.PDF",docsPath];
NSURL *targetURL = [NSURL fileURLWithPath:pathToPDF];
mcontroller.urlToFile = targetURL;
[self presentModalViewController:mcontroller animated:YES];
modalViewController.m -
- (void)viewDidLoad {
[super viewDidLoad];
NSURLRequest *request = [NSURLRequest requestWithURL:urlToFile];
[webView loadRequest:request];
}
I ended up using the documentInteractionController for this to display the PDF in Quick Look. There is a great tutorial for this in the 2010 WWDC vids.
No idea why it wasn't working well in webView, but it's smooth as silk in Quick Look.
The first thing I would look at is the PDF its self. Often people try loading huge PDF files and expect them to perform wonderfully. Can you post the PDF for us to see? How large is the file? Remember that the PDF file size is the compressed size, to display images in the PDF they will be uncompressed when stored in RAM. Its not uncommon for a 500KB image on disk to use 20 MB in memory (This applies for all raster graphics, not just PDFs). This is usually the case when PDF performance is poor from what I've seen around here.
I know you said it works great in Safari, however we don't know what all Apple does in Safari's internals vs a UIWebView. Also Safari is usually running in the background, so it is using memory even when your app is running. This reduces mem that you app can use, but when you are running safari as the foremost app, 3rd party apps aren't using much memory, so safari could potentially cache more of the PDF at a time.
I have a UIImageView, which pics up different images from the web, they are all different res. I have them to display as Aspect fit so that the whole picture displays even if it means leaving empty space on top or sides.
What i want to have the feature of doing is, once its displayed, to zoom in and out using the pinch or any other way.
NSURL* iURL = [NSURL URLWithString:tURL];
NSData* iData = [NSData dataWithContentsOfURL:iURL];
UIImage* iImage;
iImage = [UIImage imageWithData:iData];
FullScreenImage.image = iImage;
All this code is being executed on another thread, then it returns to the main thread and is displayed on a view using
[navigationController pushViewController:vFullscreen animated:YES];
Thanks for the help
You will need to use a UIScrollView as UIImageView does not offer any built-in zooming. See the Apple example entitled: ScrollViewSuite
I would like to change the standard iPhone UITextField so that it looks exactly like the search text field within the UISearchBar. You can see an example of this for the SMS text entry field within the iPhone Messages application.
I do not believe that the UITextField has a style property that will meet this requirement. Any ideas?
If you want, you can actually extract the background image from a uisearch bar and make it a stretchable image to use as the background of a UITextField. I did the same thing and it works like a charm for me. Here's the code i used to extract it:
UIImage *img = [[[self.searchBar subviews] objectAtIndex:1] background];
NSData *imgdata = UIImagePNGRepresentation(img);
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [searchPaths objectAtIndex: 0];
[imgdata writeToFile:[documentsDirectoryPath stringByAppendingPathComponent:#"searchbarbg.png"] atomically:YES];
the second subview of the uisearchbar is the actual text field; this is subject to change in any future os releases though.
this will save the PNG to the app's documents directory, where you can grab it and drop it into your project and use it as the text field's background. the left and top cap widths for the stretchable image are 15 pixels.
What about UITextBorderStyleRoundedRect?
textField.borderStyle = UITextBorderStyleRoundedRect;
You can also just use a UISearchBar and handle the text manually yourself.
It seems that this is not supported within iPhone OS 3.0. I just used a transparent UITextField with UIImageView as the background. I believe this is how the official iPhone SMS text entry works.