How to add an image in a CSV file - ios

I am writing an XCUItest and taking a screenshot of the final screen. I am saving all the result in a CSV file. Using Jenkins I am sending that CSV file as a mail.
How can I add the screenshot in my CSV file, right now I have saved the screenshot in my local device but not able to save it in a CSV file?
XCUIScreenshot* Screenshot = XCUIScreen.mainScreen.screenshot;
UIImage *image = Screenshot.image;
NSData* imageData = UIImagePNGRepresentation(image);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSData *myImageData = UIImagePNGRepresentation(image);
[fileManager createFileAtPath:#"filelocation/myimage.png" contents:myImageData attributes:nil];

There is no way you can send the image in csv file. So you can change your approach by storing the screen shots (may be for few days) on web server and send the url of image as value in CSV or Alternately, as multiple attachments to the email with image file names as references in CSV

Related

How to Store the online images and data for offline purpose

I have to build an iPhone app that check for the net connectivity and whenever it get it has an online sync with a web service, it downloads X images and saves them into the device.
Then, in offline mode, I have to load the images in collectionview for that i have to store images somewhere.Same For data also
I am using .net webservice with json response.
I was thinking about the Core Data storage, is that possible? Maybe storing images in core data database will slow down the app?
You have to download and save each received image in the application directory, then you save in CoreData the path to those images.
// Save image to disk
NSString *documentaryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/Image.png",documentaryPath];
NSData *data = [NSData dataWithData:UIImagePNGRepresentation(YOUR_IMAGE)];
[data writeToFile:filePath atomically:YES];
// Retrieve the Image
- (NSData *) imageData {
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *pngFilePath = [NSString stringWithFormat:#"%#/Image.png",docDir];
NSData *dataImage = [NSData dataWithContentsOfFile:pngFilePath];
return dataImage;
}
And use like below.
UIImage *image = [UIImage imageWithData:imageData]
May be it will help you.
Yes,it is possible,but not worthwhile.
As usually,we cache an image with follow steps:
Check whether we have load the image at the specific URL,if yes,go step 2,otherwise,go step 3
Load the image from file.
DownLoad the image,and save it to file.you should give the image file a name associated with its URL,so you can find it by the url next time.
There is a tool can do bellow work well,you can try using SDWebImage.
Yes you can store images in core data in the form of Binary Data, here is an example of how to do it link!
As far as speed is concerned core data will not slow down your app performance but it usually takes more space than sqlite, here's a link! that show comparison between core data and sqlite performance.

xcode save image to a specific file in library

I would like to save an image to the Assets library with a specific name. Or to the Photos library. I think it's the same thing. I would like to be able to save the file like "file1.png". So far I tried with writeToFile method but I couldn't get the path right.
Hope this helps you:
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/Test.png"]; // identity the home directory and file name
[data writeToFile:path atomically:YES];
NSString *imageFile = [#"file://" stringByAppendingString:path];
NSLog(#"----imageFile path--%#",imageFile);

Is it possible to download a complete directory from the server in Objective-c

I am developing an app where I have to download a complete directory. This directory will contain for example:
article1/index.html
/images/image1.png
/image2.png
/image3.png...
I have tried to download this, but I only can download the index.html.
I am using this code:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(#"Downloading Started");
NSString *urlToDownload = #"http://localhost:8888/";
NSURL *url = [NSURL URLWithString:urlToDownload];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/issue/article%#/", documentsDirectory,articleID];
NSLog(#"filepath: %#", filePath);
//saving is done on main thread
dispatch_async(dispatch_get_main_queue(), ^{
[urlData writeToFile:filePath atomically:NO];
NSLog(#"File Saved !");
});
}
});
If anyone has an idea. Let me know.
Thank you
The reason you're only getting index.html is because this is what the web server is serving you when you request that directory. If there is no index.html page, it will probably give you a directory listing - however, this is usually in HTML format and you would have to manually extract the file names from this output.
If you don't have access to this web server, your job is much harder - if you can get an HTML directory listing, you'll have to parse it yourself; if not, you can't retrieve the directory list at all. In this case, it seems like you do have access to the web server, which is great! You can make this task a lot easier for yourself...
Option one
If the data isn't likely to change, you can create a simple text document in the root directory like filelist.txt that contain a list of files/paths in the directory. Your app can first request this list, separate the entries and then start downloading each file.
Option two
You could create a simple web script (in something like PHP or your language of choice) that lists the current directory contents in a format that can be easily digested by your app - JSON, newline-separated, or anything else.
Option three
Package the directory contents in a .zip file, and have your app download and extract the archive. ZipArchive is one library that allows you to unzip files in Objective-C, and there's an easy tutorial on how to do this available on iCodeBlog.
Also, as a side note, I see you're using NSDocumentDirectory for downloaded content. Apple advises that the Documents folder should only be used for user-generated content, which does not include downloaded content. You should use NSCachesDirectory or NSApplicationSupportDirectory for data that your app downloads (note that you may have to create them first).

iOS - write jpeg file to file system

I'm new to iOS development and I'm trying to write an image as a jpeg file to the file system. From the logs I know that those file are indeed written to the file system, but when I try to save them to the camera roll they all appear black. I'm using the following code to write them as jpeg files:
[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpegPath atomically:YES];
And the following code to write to camera roll:
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
Anybody know how to verify that those jpeg files are indeed written to the file system? And what I might be doing wrong in the second line of code?
EDIT: So here is the entire method:
- (BOOL)createImagesForSlides:(NSString *)documentsPath destinationURL:(NSURL *)destinationURL
{
NSFileManager *manager = [NSFileManager defaultManager];
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:destinationURL];
NSString *folderName = [NSString stringWithFormat:#"/%#", document.title];
// create new folder
NSString *newFolderPath = [documentsPath stringByAppendingString:folderName];
BOOL result = [manager createDirectoryAtPath:newFolderPath withIntermediateDirectories:NO attributes:nil error:nil];
// create a jpeg file for each page of the pdf file
for (int i = 1; i <= document.numberOfPages; ++i) {
NSString *jpegPath = [NSString stringWithFormat:#"%#/%d.jpg", newFolderPath, i];
[UIImageJPEGRepresentation([[document pageForPageNumber:i] image], 1.0) writeToFile:jpegPath atomically:YES];
UIImageWriteToSavedPhotosAlbum([[document pageForPageNumber:i] image], nil, nil, nil);
}
return result;
}
document is a pointer to a CPDFDocument instance, and it's from some open source reader code available on github (iOS-PDF-Reader). What I basically do here is grab each page of the pdf document, generate an image, and then save them to the file system as jpeg file. Weird enough, even though there are more than 10 pages in the document, UIImageWriteToSavedPhotosAlbum only writes 5 files to camera roll. Any idea why?
It would probably be useful to see how you construct jpegPath here.
First, writeToFile:atomically: returns a BOOL, so check that for your first indication of success or failure.
There are a couple of ways you can verify that the image is written to the file system. If you are running on a device use something like iExplorer to access the file system and look at the file written. Since it is NSData* you can cheek the file size to make sure it looks reasonable. On the simulator, dig into the folder structure under ~/Library/Application Support/iPhone Simulator/ and examine the file. Without looking into the filesystem itself try reading the image back into another UIImage (imageWithData: in your case since you are writing a NSData* object).
There doesn't appear to be anything wrong with your UIImageWriteToSavedPhotosAlbum call according to the docs. It is OK for the last 3 arguments to be nil (all are marked as optional), you just have to be sure the UIImage is valid. Have you set a breakpoint to be sure you have a valid image (Xcode Quick Look feature)?

Converting an iPhone IOS Video File Uploader to work with a file stored in document directory

This is my first real project. I have an app that captures several seconds of video using AVFoundation, outputs this to a file in the documents directory and lets the user preview the video before they upload it using HTTP and a PHP script on my website.
All the video capture and preview work perfectly but I am stuck on uploading the video file.
I learnt a lot from this simpleSDK video which shows how to achieve the desired effect using a video file stored in the apps main bundle.
The code from the tutorial that set up videoData ready to upload originally looked like this:
NSData *videoData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Movie" ofType:#"mov"]];
NSString *urlString = #"http://www.iphonedevnation.com/video-tutorial/upload.php";
The filename of the video file that I need to upload is always unique and generated using CFUUIDCreateString. I join this string to the path for the documents directory, add ".mov" to the end of it and save it into a text file for retrieving later.
This all works as I am able to retrieve the filename from the file and use it to preview the movie clip elsewhere in the app.
My path is in an NSString, that I have tried converting to NSURL and removing the file suffix to get it to work with the NSData *videoData.........line but it doesn't compile, I get an "No known class method for selector 'dataWithContentsOfFile:ofType.' error. I am targeting iOS 5 and using Xcode 4.3 with ARC and Storyboards.
I've been at this for best part of 5 hours now so hopefully someone can help. My code, which included tips from elsewhere on converting from a NSString to NSURL follows:
NSString *content = [[NSString alloc] initWithContentsOfFile:lastSavedTalentFilenamePath
usedEncoding:nil
error:nil];
NSLog(#"content=%#",content);
//Need to now remove the '.mov' file type identifier
NSString *shortContent= [content substringToIndex:[content length]-4];
NSLog(#"***************shortContent***************%#", shortContent);
NSURL *convertedContent = [NSURL fileURLWithPath:shortContent];
NSLog(#"***************convertedContent***********%#",convertedContent);
NSData *videoData = [NSData dataWithContentsOfFile:convertedContent ofType:#"mov"];];
There is no NSData method called dataWithContentsOfFile:ofType:
The methods available are:
+ dataWithContentsOfFile:
+ dataWithContentsOfFile:options:error:
both of which take the file location as an NSString so there's not need to convert to an NSURL

Resources