xcode save image to a specific file in library - ios

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);

Related

How to get file in a library iOS

I am trying to construct a library in IOS.
In my library ,I use a method -(void)loadNewData to load some data EX:test.csv.
But when I export my library and let the other app use my library to excute the loadNewData method.
I found it can not be load correctly. It seems that because there is not such a file test.csv in the app.
How should I revise my code? Or where should I notice when I export the library?
Thank you for watch my question.
And sorry for my poor English.
Here is the a part of code of the loadNewData:
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
NSString *Rawsurveys=[resourcePath stringByAppendingPathComponent:#"test.csv"];
NSString *RawsurveyResults;
if([[NSFileManager defaultManager]fileExistsAtPath:Rawsurveys])
{
NSLog(#"find file");
NSFileHandle *RawfileHandle=[NSFileHandle fileHandleForReadingAtPath:Rawsurveys];
RawsurveyResults=[[NSString alloc]initWithData:[RawfileHandle availableData] encoding:NSUTF8StringEncoding];
[RawfileHandle closeFile];
}
You could probably create a bundle within your library and add you test.csv there.
Then you can access your test.csv from your own bundle.
Example of how I use images from my bundle. [UIImage imageNamed:#"myBundle.bundle/myImage"]. You could use the somewhat same for your file

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).

Can I access all images in a .xcassets at once?

I have a big amount of images in a Images.xcassets and I need to load them all in a UITableView. But I don't wat to write each name for the [UIImage imageNamed:#"imageName"].
What i'm looking for is something like:
NSArray *images = [MagicClass loadAllImagesIn:#"images.xcassets"];
Do you know if this is even possible? If not maybe i'll create a .bundle...
Thanks!!
Images.xcassets copies all the images in bundle, and it mixes up with other images, better follow the instructions in image below
By adding the folder with the option Create folder reference will actually create a folder in main bundle with its content, and all your images will be separated. And using the following code you can access all the images.
NSString *dirPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:#"images"];
NSError * error;
NSArray * images = [[NSFileManager defaultManager]
contentsOfDirectoryAtPath:dirPath error:&error];
Hope it helps.

quicklook not showing image but has right data

i'm using quicklook to preview an image saved locally. when i run the app, quicklook read the right file and has the right image memory but doesn't preview it. it shows me something like this.
in my code i'm passing an NSURL to this delegate method
(id)previewController:(QLPreviewController *)previewController previewItemAtIndex:(NSInteger)idx
so i guess everything is in place. but what could i missing?
EDIT:
to creath the path i do it by the following:
filePath = [NSString stringWithFormat:#"%#/LeBaccPictureDownloadedTemperoraly", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]];
and in the delegate method i mentioned i return the url by this:
[NSURL fileURLWithPath:filePath];
does filePath = [#"~/Documents/LeBaccPictureDownloadedTemperoraly" stringByExpandingTildeInPath]; change anything? Does the file have an extension? Try to show the file in an UIImageView to make sure the file is not broken. Whatch the file format xD
Like this?

Image retrieve from folder with folder name in iOS

I have all my images in folder name ImageMe and I like to retrieve image with this name like
[UIImage imageNamed:#"ImageMe/bulk-female.jpg"];
how I get this approach, because I rereiving path name from data base, and in that format it's coming.
If you do not want to modify these images, just create folder name "Resource" under supporting files and save ImageMe folder with your images there. Then use below code to get path of image folder.
NSString *theResourcePath = [NSBundle mainBundle] resourcePath];
NSString *theFilePath = [theResourcePath stringByAppendingPathComponent:#"ImageMe/bulk-female.jpg"];
If you have the images in the main bundle you can get them with
[UIImage imageNamed:#"theNameOfTheImage_WithOutDirecotry"];.
You have to specify only the image name not the directory. This is because when the application is packed in .ipa only the images are copied (the images that are in the Copy Bundle Resources) not the directories.
If you want to pack the images in to one folder you have to create a new bundle but you can not use imageNamed you have to use initWithContentsOfFile:(NSString *)path
Here are some docs UIImage, NSBundle, Bundle Programming Guide
If your all images are present in bundle then you can directly get that images from name.
like this
[UIImage imageNamed:#"1.jpg"];
[UIImage imageNamed:#"2.jpg"]
[UIImage imageNamed:#"3.jpg"]
directly get the images using their names.
And if you want to laod that images to particular folder of bundle.
NSString *Path = [NSBundle mainBundle] resourcePath];
NSString *FilePath = [Path stringByAppendingPathComponent:#"ImageMe/bulk-female.jpg"];
try this one this might be helpful to you.
Add your images in a floder & than add that folder to your project like this
after adding the folder to your project, you can access your images with the following code.
NSString *bundlePath = [NSBundle mainBundle] bundlePath];
NSString *imagePath = [bundlePath stringByAppendingPathComponent:#"ImageFolder/imagename.jpg"];
Hope this will solve your problem

Resources