Image not access in iOS 6 from document library? - ios

I have to access image from document library in iOS 6 .
strImgSign = [NSString stringWithFormat:#"%#/Documents//%#", NSHomeDirectory(),strImgSign];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString *strImgSign = [[arrNameFinal objectAtIndex:indexPath.row] objectForKey:#"signimage"];
/var/mobile/Applications/D912ACC0-5B23-49B2-B9F1-E45ECD713553/Documents//docSign6167Feb13,2015-12.03.26.png
But image access in iOS 7,8 is perfect Working.

You should not build the directory from the NSHoneDirectory or use path separators in you string.
Correct way to build directories:
NSString *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
imagePath = [imagePath stringByAppendingPathComponent:strImgSign];
But even better and only correct way is to use the correct directory:
NSString *documentPath =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *imagePath = [documentPath stringByAppendingPathComponent:strImgSign];

There is an extra / in your path:
strImgSign = [NSString stringWithFormat:#"%#/Documents//%#", NSHomeDirectory(),strImgSign];
Change that to:
strImgSign = [NSString stringWithFormat:#"%#/Documents/%#", NSHomeDirectory(),strImgSign];
But as mentioned by #rckoenes, it's not the correct way of building file system paths. If your image is located in document directory you can use:
NSString *docDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *yourImagePath = [docDirectory stringByAppendingPathComponent:strImgSign];
But I'm not sure how it worked for you in iOS 7 and iOS 8

NSString *filename = [filepart lastObject];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString documentsDirectory = [paths objectAtIndex:0];
NSString inboxPath = [documentsDirectory stringByAppendingPathComponent:#"Inbox"];
NSString *filePath = [inboxPath stringByAppendingPathComponent:filename];

Related

Unzip Files in iOS

I have a .zip folder which contains some images. I want it to unzip into Documents directory in iOS device. I tried with the following link,
download and unzip file in iOS-Stack Overflow
Here is the code that I tried in my app,
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filepath = [documentsDirectory stringByAppendingPathComponent:#"Obj.zip"];
ZipArchive *zipArchive = [[ZipArchive alloc] init];
[zipArchive UnzipOpenFile:filepath Password:#""];
[zipArchive UnzipFileTo:[documentsDirectory stringByAppendingPathComponent:#"Obj"] overWrite:YES];
[zipArchive UnzipCloseFile];
And then I printed the all files in Document Directory, but the Obj.zip is the only file(Obj.zip is the file that wants to unzip) that exist in the Documents Directory. How can I done this?
Thanks in Advance!
Use SSZipArchive. Here is a sample code. https://github.com/soffes/ssziparchive
Also I believe the unzipping would create a folder in documents directory which would contain the files. Please check that
NSString *zipPath = [[NSBundle mainBundle] pathForResource:zipFileName ofType:#"zip"];
NSString *destinationPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
[SSZipArchive unzipFileAtPath:zipPath toDestination:destinationPath];
Update : Get zip file from documents directory
NSString *filename = #"MyZipFile.zip";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString * zipPath = [documentsDirectory stringByAppendingPathComponent:filename];
NSString *destinationPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
[SSZipArchive unzipFileAtPath:zipPath toDestination:destinationPath];

How to make a path in objective C?

I'm working with Objective C (IOS) and I found this code:
Person *aPerson = <#Get a Person#>;
NSString *archivePath = <Path for the archive#>;
BOOL success = [NSKeyedArchiver archiveRootObject:aPerson toFile:archivePath];
I would like to know what the archivePath should be to save the file in documents directory.
Thank you
To figure out archivePath:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *archivePath = [documentsPath stringByAppendingPathComponent:#"Person"];

ipad - write NSString to text file

I have text file text.txt in my xcode project and I need to write NSString init from my iOS app. I've tried this code
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex: 0];
NSString *docFile = [docDir stringByAppendingPathComponent: #"text.txt"];
NSString *content = #"Test";
[content writeToFile: docFile atomically: NO];
But it doesn't work for me... So can you help me with it?
this worked for me
//Method writes a string to a text file
-(void) writeToTextFile{
//get the documents directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the documents directory:
NSString *fileName = [NSString stringWithFormat:#"%#/text.txt",
documentsDirectory];
//create content - four lines of text
NSString *content = #"Test";
//save content to the documents directory
[content writeToFile:fileName
atomically:NO
encoding:NSStringEncodingConversionAllowLossy
error:nil];
}

Try to write a string to file [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Problem in writing to a file in application bundle
I am trying to write a String to a file using the following code :
NSString *file = [[NSBundle mainBundle] pathForResource:#"settings" ofType:#"txt"];
NSLog(#"Path : , %#", file);
NSError *error=NULL;
NSString *data=#"teleiwne";
[data writeToFile:file atomically:YES encoding:NSUTF8StringEncoding error:&error];
The print out of Path is : /Users/kd/Library/Application Support/iPhone Simulator/6.0/Applications/763B8245-CB1A-4BDE-85CF-19AEFA411DB8/testKremala.app/settings.txt
I don't get any error but nothing is written to file
I tried the following code and it worked :
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:#"set.txt"];
NSString *data=#"Kostas";
[data writeToFile:appFile atomically:YES];
NSString *myData = [NSString stringWithContentsOfFile:appFile];
NSLog(#"Data : %# ",myData);
You aren't allowed to write to the App directory. Try writing to the Documents directory inside that app folder.
NSString *data = #"testData";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:#"settings.txt"];
[data writeToFile:appFile atomically:YES];

How to Read a file from documents directory in iOS

I need to search a file in documents directory of iOS but i don't know the extension of that file. How to search the file without extension.
Below is the code if you know the filename.extension(abc.txt). How can i search for the same file with jsut filename(abc)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"abc.txt"];
NSString *content = [NSString stringWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding error:NULL];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSString *fileWithExt = nil;
for (NSString *file in directoryContent )
{
if([file stringByDeletingPathExtension] isEqualsTo:#"abc"]
{
fileWithExt = file;
break;
}
}
P.S. : The fileWithExt will contain the first file with name abc

Resources