Saving screenshot to sandbox - ios

Is there any way to save a screenshot to the application's sandbox or some where that can be easily found to add to an email attachment? currently im saving it in the camera roll but can't seem to be able to get it to attach to my email
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [path objectAtIndex:0];
NSString *png = #".png";
NSString *filename = [drawquestion stringByReplacingOccurrencesOfString:#" " withString:#""];
NSString *link = [NSString stringWithFormat: #"%#%#%#", documentDirectory,filename,png];
NSString *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:link];
NSLog(link);
UIImageWriteToSavedPhotosAlbum(viewImage, drawquestion, nil, nil);

Here are 2 bare bones examples of how to attach an image to an email.
// with UIImage * image;
MFMailComposeViewController * mfcvc = [[[MFMailComposeViewController alloc] init] autorelease];
NSData * imageData = UIImagePNGRepresentation(image);
[mfcvc addAttachmentData:imageData mimeType:#"image/png" fileName:#"demo"];
[self.viewController presentModalViewController:mfcvc animated:YES];
// with UIImage * image; and float compression_quality; between 0.0 and 1.0
MFMailComposeViewController * mfcvc = [[[MFMailComposeViewController alloc] init] autorelease];
NSData * imageData = UIImageJPEGRepresentation(image, compression_quality);
[mfcvc addAttachmentData:imageData mimeType:#"image/jpeg" fileName:#"demo"];
[self.viewController presentModalViewController:mfcvc animated:YES];
To save the image in the documents directory in the app's sandbox
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [path objectAtIndex:0];
NSString *png = #".png";
NSString *filename = [drawquestion stringByReplacingOccurrencesOfString:#" " withString:#""];
NSString *imagePath = [documentDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#%#",filename, png]];
[imageData writeToFile:imagePath atomically:YES];
PS
I cleaned up the construction of the path, it seems your code has a path like
/var/mobile/Applications/APP_ID//var/mobile/Applications/APP_ID/Documents/filename.png

You should check out writeToFile:atomically: you get your image data, and write it to a file, this will save to the application's sandbox.
Check out this for some example code: http://blog.objectgraph.com/index.php/2010/04/05/download-an-image-and-save-it-as-png-or-jpeg-in-iphone-sdk/
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSLog(#"%#",docDir);
NSLog(#"saving png");
NSString *pngFilePath = [NSString stringWithFormat:#"%#/test.png",docDir];
// You should swap data for your image data
NSData *data = [NSData dataWithData:UIImagePNGRepresentation(image)];
[data writeToFile:pngFilePath atomically:YES];
You can then use the addAttachmentData:mimeType:fileName: method on MFMailComposeViewController class to attach an attachment (actually you don't even need to save the image to disk):
MFMailComposeViewController * mailVC = [[MFMailComposeViewController alloc] init];
NSData *data; // this is the data from earlier
[mailVC addAttachmentData:data mimeType:#"image/png" fileName:#"myfilename"];
[self presentModalViewController:mailVC animated:YES];
Be sure to have a correct MIME type set here. The file name you can set to what you want, it is the name of the file in the attachment as seen by the recipient.

Related

How to fetch multiple images from server and save and show them in iPhone app?

I am using following code for getting single image. But, how to do it for multiple images?
This is my code :
-(void)viewDidLoad
{
[super viewDidLoad];
[self performSelectorInBackground:#selector(loadImageIntoMemory) withObject:nil];
}
-(void)loadImageIntoMemory {
NSLog(#"Beginning download");
NSString *temp_Image_String = [[NSString alloc] initWithFormat:#"http://www.thewavestore.com/appproductimage/"];
NSURL *url_For_Ad_Image = [[NSURL alloc] initWithString:temp_Image_String];
NSData *data_For_Ad_Image = [[NSData alloc] initWithContentsOfURL:url_For_Ad_Image];
UIImage *temp_Ad_Image = [[UIImage alloc] initWithData:data_For_Ad_Image];
[self saveImage:temp_Ad_Image];
UIImageView *imageViewForAdImages = [[UIImageView alloc] init];
imageViewForAdImages.frame = CGRectMake(0, 0, 320, 480);
imageView.image = [self loadImage];
[self.view addSubview:imageView];
}
-(void)saveImage:(UIImage *)image {
NSLog(#"Saving");
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent: #"DSC02077129HT41.5W20L24WT18.555RS28775.jpg" ];
NSData* data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
}
-(UIImage *)loadImage {
NSLog(#"Got the data!");
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:#"DSC02077129HT41.5W20L24WT18.555RS28775.jpg" ];
UIImage* image = [UIImage imageWithContentsOfFile:path];
return image;
}
Please help to over come from this issues,if any good idea please suggest me.
First save your images with unique names (i am using current time) and save this names to an array
NSData *imageData = UIImagePNGRepresentation(chosenImage);
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
// save image with currentdate and time in the name
NSString * imageName = [NSString stringWithFormat:#"name_%#.png",[dateFormatter stringFromDate:[NSDate date]]];
NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
//add all the images names to the imagepath array
[imagePathArray addObject:imageName];
//write image into file
[imageData writeToFile:fullPathToFile atomically:YES];
then retrive images from image path array
for (NSString*path in imagePathArray ) {
NSArray *filepart = [path componentsSeparatedByString:#"."];
NSString *filename = [filepart objectAtIndex:0];
NSString *extension = [filepart objectAtIndex:1];
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = [NSString stringWithFormat:#"%#/%#",
documentsDirectory,path];
//NSString *filePath = [[NSBundle mainBundle] pathForResource:filename ofType:extension];
NSData *fileData = [NSData dataWithContentsOfFile:fileName];
}
1. Store the server images in to array.
NsMutableArray *arrImages=[[nsmutableArray alloc]init];
2.Get the count of the array to run this in for loop.
for(int i=0;i<[arrImages count];i++)
{
NSLog(#"Saving");
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent: arrImages[i]];
NSData* data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
}
To convert it to JPEG
NSData=UIImageJPEGRepresentation(UIImage,1.0);

Unable to download/read a file in documents path of my ios app

I am using the below code to download a ebook from url and store into my device. While this works in emulator, in my ipad I am getting error saying file is not available. I am not able to read the file in the respective path of the device. please help..
NSString *urls = [NSString stringWithFormat: #"http://hungerin.com/?download_file=%#&order=%#&email=%#&key=%#", BookID, orderKey, email, downloadId];
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: urls]];
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#""]];
NSString *filePathAppend = [NSString stringWithFormat: #"/testPubb.app/Documents/%#.epub",BookID];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:filePathAppend];
[pdfData writeToFile:filePath atomically:YES];
The way you are getting the app document directory path is not correct. Try this:
NSString *urls = [NSString stringWithFormat: #"http://hungerin.com/?download_file=%#&order=%#&email=%#&key=%#", BookID, orderKey, email, downloadId];
NSData *pdfData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urls]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.epub",BookID];
[pdfData writeToFile:filePath atomically:YES];
Let me know if you have questions.

How do I download NSData, change metadata, and convert it back to NSData?

I'm trying to download an .mp4/.m4a, change the metadata (background image), and convert it back to NSData so it can be copied to clipboard. As you can see by the code below. _artworkData and _previewData are not empty.
UIImage *artworkImage = [UIImage imageWithData:_artworkData];
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory=[paths objectAtIndex:0];
NSString *path = [documentDirectory stringByAppendingPathComponent:[NSString stringWithFormat: #"File.mp4"]];
NSString *path = [NSString stringWithFormat:#"path"];
[_previewData writeToFile:path atomically:YES];
ANMovie* file = [[ANMovie alloc] initWithFile:path]; // or .mp4
NSData* jpegCover = UIImageJPEGRepresentation(artworkImage, 1.0);
_imageView.image = [UIImage imageWithData:jpegCover];
ANMetadata* metadata = [[ANMetadata alloc] init];
metadata.albumCover = [[ANMetadataImage alloc] initWithImageData:jpegCover type:ANMetadataImageTypeJPG];
[file setMovieMetadata:metadata];
[file close];
_previewData = [NSMutableData dataWithContentsOfFile:path];
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSDictionary *imageItem=#{#"public.mpeg-4-audio":self.previewData};
NSDictionary *textItem=#{#"public.plain-text":self.linkData};
pasteboard.items=#[imageItem,textItem];
The following code does not work and stops on the following line logging a random 10 digit number.
NSDictionary *imageItem=#{#"public.mpeg-4-audio":self.previewData};
Is this not working because I have to convert .m4a to .mp4 or something? It seems editing metadata on either or would work.

How can I add more images in NSDocumentDirectory?

First I stored image path from web service into SQLITE, and then I retrieve it back using local path. I am using NSMutableArray. I've successfully stored one image in NSDocumentDirectory, but I have one issue, How can I store more than one images in NSDocumentDirectory?
My code is,
if ([[[node childAtIndex:counter] name] isEqualToString:#"Column5"])
{
NSString *str = [[node childAtIndex:counter] stringValue];
[Col5 addObject:str];
NSString *myString = [Col5 componentsJoinedByString:#","];
NSString *aString = [NSString stringWithFormat:#"http://webserviceurl/%#",myString];
NSURL *url= [NSURL URLWithString:aString];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:strCol4];
UIImage *image = tmpImage;
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];
}
For this line:
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:strCol4];
Just use a different path component.

Attaching a recorded .caf file locatted in documents directory to mail iOS Xcode

I have files recorded by the user in the documents directory... displayed in a uitableview...
I placed this code in my didSelectRowAtIndexPath... Why doesn't this work?
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#",[directoryContents objectAtIndex:indexPath.row]]];
NSURL *audioURL = [NSURL URLWithString:fileName];
NSData *audioData = [NSData dataWithContentsOfURL:audioURL];
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer addAttachmentData:audioData mimeType:#"audio/caf" fileName:fileName];
You are creating the URL incorrectly. You need to use fileURLWithPath, not URLWithString.
Also, this:
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#",[directoryContents objectAtIndex:indexPath.row]]];
Should be:
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:[directoryContents objectAtIndex:indexPath.row]];
There is no need for the string format.
Last thing. Th file name passed to the addAttachment method should be just a file name, not a full pathname.

Resources