Get only image files from directory - iphone

I am storing image file in cache directory . Later I want to get all image file list from cache directory. I am using following code to get all files.
[fileManager contentsOfDirectoryAtPath:pathForCacheDirectory error:&error]
How to separate image files from this. Image file can be any format.
Thanks in advance.

// Store your supported image Extensions
NSArray *extensionList = [NSArray arrayWithObjects:#"jpg", #"jpeg", #"png", #"gif", #"bmp", nil];
// Grab the content Directory
NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:pathForCacheDirectory error:&error];
NSMutableArray *listOfImageFiles = [NSMutableArray arrayWithCapacity:0];
// Check for Images of supported type
for(NSString *filepath in contents){
if ([extensionList containsObject:[filepath pathExtension]])
{
// Found Image File
[listOfImageFiles addObject:filepath];
}
}
NSLog(#"Lisf of Image Files : %#",listOfImageFiles);

a brutal way is to enum all extensions you consider it to be an image.
a better way is using UTI, check this Get the type of a file in Cocoa

You can filter file using extensions.
NSArray *contents = [fileManager contentsOfDirectoryAtPath:pathForCacheDirectory error:&error];
for(NSString *filepath in contents){
if ([[filepath pathExtension] isEqualToString: #"png"]) {
// Your code
}
}

Try this, hope this will help.
NSArray * contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:YOURPATH error:NULL];
NSMutableArray * onlyImages = [[NSMutableArray alloc]init];
for (NSString * contentPath in contents) {
NSString * lastPath = [contentPath pathExtension];
if ([lastPath isEqualToString:#"jpg"] || [lastPath isEqualToString:#"jpeg"] || [lastPath isEqualToString:#"png"] || /* any other */ ) {
[onlyImages addObject:contentPath]; // only images
}
}

See this & check:
CFStringRef fileExtension = (CFStringRef) [file pathExtension];
CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);
if (UTTypeConformsTo(fileUTI, kUTTypeImage)) NSLog(#"It's an image");
else if (UTTypeConformsTo(fileUTI, kUTTypeMovie)) NSLog(#"It's a movie");
else if (UTTypeConformsTo(fileUTI, kUTTypeText)) NSLog(#"It's text");
else NSLog(#"It's audio");

Related

How to zip a folder using ZipArchive in iphone?

I have a folder in the documents directory that needs to be zipped.I was able to zip regular files but i was not able to zip folders.
I referred the following link
How to zip folders in iPhone SDK?
But here the files in the folder are zipped separately.I would like to zip the entire folder instead of having to deal with exploring the contents(files/folders) inside the directory and zipping them one by one.
Is it possible to do this with the ZipArchive library. If it is could some one please explain by posting the necessary code?
Thank You.
You can't do that with ZipArchive but have a look at SSZipArchive it has a + (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath;
method you could use.
// Path to store Zip
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* dPath = [paths objectAtIndex:0];
NSString* zipfile = [dPath stringByAppendingPathComponent:#"test.zip"] ;
// File Tobe Added in Zip
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"GetAllCardList" ofType:#"xml"];
NSString *fileName = #"MyFile"; // Your New ZipFile Name
ZipArchive* zip = [[ZipArchive alloc] init];
if([zip CreateZipFile2:zipfile])
{
NSLog(#"Zip File Created");
if([zip addFileToZip:filePath newname:[NSString stringWithFormat:#"%#.%#",fileName,[[filePath lastPathComponent] pathExtension]]])
{
NSLog(#"File Added to zip");
}
}
try SSZipArchive
+ (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath keepParentDirectory:(BOOL)keepParentDirector;
I used
let success = SSZipArchive.createZipFileAtPath(zipFullFilename, withContentsOfDirectory:dataDir, keepParentDirectory:true)
to create zip folder. It is worked.
You just need to get the contents of the dir prior to adding all the files to the zip as in the example. Adding them one by one is the same thing in code so just get a list then run through the list.
NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *dirContents = [fm contentsOfDirectoryAtPath:bundleRoot error:nil];
If you are looking for specific files you can also use a predicate to filter the results
NSPredicate *filter = [NSPredicate predicateWithFormat:#"self ENDSWITH '.png'"];
NSArray *pngs = [dirContents filteredArrayUsingPredicate:filter];
#implementation ZipArchive(Util)
+ (BOOL)makeZipFile:(NSString*)filepath withContentInDirectory:(NSString*)directory
{
ZipArchive* zip = [[ZipArchive alloc] initWithFileManager:[NSFileManager defaultManager]];
if ([zip CreateZipFile2:filepath]){
[self enumDirectory:directory zipFile:zip];
return [zip CloseZipFile2];
}
return NO;
}
+ (void)enumDirectory:(NSString*)directory zipFile:(ZipArchive*)zip
{
NSArray* resourceKeys = #[NSURLIsDirectoryKey];
NSDirectoryEnumerator<NSURL *> * enumerator = [[NSFileManager defaultManager] enumeratorAtURL:[NSURL fileURLWithPath:directory] includingPropertiesForKeys:resourceKeys options:NSDirectoryEnumerationSkipsHiddenFiles errorHandler:^BOOL(NSURL * _Nonnull url, NSError * _Nonnull error) {
return NO;
}];
for (NSURL* url in enumerator) {
NSDictionary<NSString *, id> *resourceValues = [url resourceValuesForKeys:resourceKeys error:nil];
BOOL isDirectory = [[resourceValues objectForKey:NSURLIsDirectoryKey] boolValue];
if (isDirectory) {
continue;
}
NSInteger len = [directory length];
NSString* subpath = [url.path substringFromIndex:len];
if ([subpath rangeOfString:#"/"].location == 0) {
subpath = [subpath substringFromIndex:1];
}
[zip addFileToZip:url.path newname:subpath];
}
}
#end

unable to omit #".DS_Store" file while fetching contents of Document Drectory

I am working on storing a list of audio files into my document directory and then fetching them.
It gives me a list of audio files along with this it gives me a file with name #".DS_Store". While fetching content I want to leave this file of documents directory.
Is there any way I can get rid of this while fetching the audio list other than removing this from array or apply a #".DS_Store" check.
What exactly is the reason for this.?
#pragma mark - Saving Audio in Document Directory
-(void)saveAudioinDocumentDirectory:(ASIHTTPRequest *)theRequest
{
/*save the Audio file in Document Directory */
NSFileManager *fileManager=[NSFileManager defaultManager];
NSLog(#"GOT THE SIZe OF AUDIO %d",[[theRequest responseData] length]);
NSLog(#"AUDIO ID IS %#",[[theRequest userInfo] valueForKey:#"audioIndex"]);
/*Get the Path to Application Documents Directory*/
NSArray *docDir=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
/*append the neccessary file extension */
NSString *filepathStr=[NSString stringWithFormat:#"/%#/%#.mp3",docDir,[NSString stringWithFormat:#"%#",[[theRequest userInfo] valueForKey:#"audioIndex"]]];
/*Check if my crrent file exists in the Documents Directory*/
if(![fileManager fileExistsAtPath:filepathStr])
{
/* file doesnt exists */
/*create a NSdata of File*/
NSData *data=[NSData dataWithData:[theRequest responseData]];
NSLog(#"%#",filepathStr);
if ([data length] >0 ){
/*write the File at the Location in Documents Directory */
[data writeToFile:filepathStr atomically:YES];
NSLog(#"Successfully saved the file to %#", filepathStr);
}
else if([data length] == 0)
{
NSLog(#"Nothing was downloaded.");
}
}
/*After saving fetch the path til documents Directory*/
NSArray *folders = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
/*Get the Path for Files */
NSString *s=[folders objectAtIndex:0];
/*Fetch the list of Files stored in Documents Directory*/
NSArray *contents = [fileManager contentsOfDirectoryAtPath:s error:NULL];
NSLog(#"TOTAL NUMBER OF AUDIO FILES %d %#",[contents count],[contents objectAtIndex:0]);
if([Audiolistforstr isEqualToString:#"AudioListForIntro"])
{
// NSLog(#"Audiolistforstr#"IntroThirdRow" in reading audio from document Intro IS %#",Audiolistforstr);
/*Intro*/
[AudioListArrForIntro removeAllObjects];
[AudioListArrForIntro addObjectsFromArray:contents];
if([AudioListArrForIntro containsObject:#".DS_Store"])
{
[AudioListArrForIntro removeObject:#".DS_Store"];
}
NSLog(#"FINAL LIST %#",AudioListArrForIntro);
}
else if([Audiolistforstr isEqualToString:#"AudioListForCredits"])
{
// NSLog(#"Audiolistforstr#"IntroThirdRow" in reading audio from document credit IS %#",Audiolistforstr);
/*credits*/
[AudioListArrForCredits removeAllObjects];
[AudioListArrForCredits addObjectsFromArray:contents];
if([AudioListArrForCredits containsObject:#".DS_Store"])
{
[AudioListArrForCredits removeObject:#".DS_Store"];
}
NSLog(#"FINAL LIST %#",AudioListArrForCredits);
}
/* Did we find anything? */
if([Audiolistforstr isEqualToString:#"AudioListForIntro"])
{
// NSLog(#"Audiolistforstr#"IntroThirdRow" in reading audio fromRELOADNG TABLE Intro IS %#",Audiolistforstr);
/*Intro*/
if ([AudioListArrForIntro count] == 0)
{
}
else
{
UIView *vw=(UIView *)[self.view viewWithTag:ViewAddAudioIntroTag];
[(UITableView *)[vw viewWithTag:tblIntroAudioListTag] reloadData];
}
}
else if([Audiolistforstr isEqualToString:#"AudioListForCredits"])
{
// NSLog(#"Audiolistforstr#"IntroThirdRow" in reading audio fromRELOADNG TABLE Intro IS %#",Audiolistforstr);
/*Credits*/
if ([AudioListArrForCredits count] == 0)
{
}
else
{
/*AudioListForCredits*/
UIView *vw=(UIView *)[self.view viewWithTag:ViewAddAudioCreditsTag];
[(UITableView *)[vw viewWithTag:tblCreditsAudioListTag] reloadData];
}
}
}
Any help would be appreciated.
Thanks
Vikas
You can check for .DS_Store after NSArray *docDir=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; line........that is,
NSMutableArray * dirContents = [[NSMutableArray alloc] initWithArray:docDir];
if([docDir containsObject:#".DS_Store"])
{
[dirContents removeObject:#".DS_Store"];
}
By this, dirContents removes the entry of .DS_Store.
Filter your document directory contents. For example, if you are having audio files with extension of .mp3, then you can get all the mp3 files as below:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:NULL];
directoryContent = [directoryContent filteredArrayUsingPredicate:
[NSPredicate predicateWithFormat:#"pathExtension ==[c] %#", #"mp3"]];
This will omit all other files than the mp3 files..
All the best!!!
The API you're using:
NSArray *contents = [fileManager contentsOfDirectoryAtPath:s error:NULL];
returns all files found at the path, which would include the ".DS_Store" file.
I'd recommend assigning "contents" to a mutable array, e.g.:
NSMutableArray * contents =
[[NSMutableArray alloc] initWithArray: [fileManager contentsOfDirectoryAtPath:s error:NULL]];`
and iterate through the array to find and removing any and all files that don't have ".mp3" as a path extension.
I'd also recommend not starting any variable with an upper case letter (e.g. instead of "Audiolistforstr", use "audiolistforstr" or even better, "arrayofAudioFiles"). Objective C best practice is to start all variables and methods with lower case letters.
Its Working well..
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *imageFilenames = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];
for (int i = 0; i < [imageFilenames count]; i++)
{
NSString *imageName = [NSString stringWithFormat:#"%#/%#",documentsDirectory,[imageFilenames objectAtIndex:i] ];
if (![[imageFilenames objectAtIndex:i]isEqualToString:#".DS_Store"])
{
UIImage *myimage = [UIImage imageWithContentsOfFile:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:_myimage];
}
}

Convert captured image in to a separate png file, and insert that png file name as a string in to sqlite data base and retrieve it

In my iPhone app I need to capture a picture and save it in to the sqlite database.
NSString *insertQuery = [NSString stringWithFormat:#"INSERT INTO ProfileTable (NAME, MOBILE, EMAIL, ProfilePICTURE) VALUES(?, ?, ?, ?)"];
const char *insert = [insertQuery UTF8String];
if(sqlite3_prepare_v2(contactsDB, insert, -1, &insertStatement, NULL) != SQLITE_OK) {
NSLog(#"Error while creating insert Statement");
}
else
{
sqlite3_bind_text(insertStatement, 1, [name UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insertStatement, 2, [mobile UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insertStatement, 2, [email UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_blob(insertStatement, 4, [profilePic bytes], [contactImage length], NULL);
}
}
// Same Query for insert statement also
I saved that file in the form of blob type but that cause to occupy huge memory and causes to memory warnings.
So i have an idea that.
I want to save the captured image as a png file for suppose "image1.png" and insert that title "image1.png" as a varchar type and the retrieve that file having that name when we want to display.
any help how to do this
Get the document directory (or other directory that you might want to store the images):
/**
Returns the URL to the application's Documents directory.
*/
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
Store image as png:
NSURL *documentsDirURL = [self applicationDocumentsDirectory];
NSURL *fileURL = [documentsDirURL URLByAppendingPathComponent:#"some-image-name.png"];
NSData* data = UIImagePNGRepresentation(image);
[data writeToURL:fileURL atomically:YES];
NSString *filePath = [fileURL path];
// now save your filePath as string to sqlite
Note, you'll have to fill in details, such as where the appropriate directory is in the filesystem, a naming convention, generating filenames unless they exist already etc.
EDIT:
Here's some help with serial naming of the images. There are a lot of different solutions.
Create an entry in the NSUserDefaults with the key #"lastImageNumber". When you need a new image name:
NSInteger imageNumber = [[NSUserDefaults standardUserDefaults] integerForKey:#"lastImageNumber"] + 1;
NSString *imageName = [NSString stringWithFormat:#"image%05d.png",imageNumber];
// save the new imageNumber in the `NSUserDefaults`
[[NSUserDefaults standardUserDefaults] setInteger:imageNum ForKey:#"lastImageNumber"];
Add int counter in appDelegate.h file
Also add In application didFinishLaunchingWithOptions method:
counter = 0;
if(![[NSUserDefaults standardUserDefaults] integerForKey:#"ImageNumber"])
{
[[NSUserDefaults standardUserDefaults] setInteger:counter ForKey:#"ImageNumber"]
}
else
{
counter = [[NSUserDefaults standardUserDefaults] integerForKey:#"ImageNumber"];
}
Also add this method in appDelegate.h file
+ (NSString *) applicationDocumentsDirectory
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
return basePath;
}
Now when ever adding captured image in database firstly add int Doc Directory like this:
appDelegate = (appDelegate *)[[UIApplication sharedApplication] delegate];
NSString *strDocDirPath = [appDelegate applicationDocumentsDirectory];
NSString *fileName = [NSString stringWithFormat:#"Image%03d.png",appDelegate.counter];
strDocDirPath = [strDocDirPath stringByAppendingPathComponent:fileName];
NSData* imgdata = UIImagePNGRepresentation(yourCapturedImage);
[imgdata writeToFile:strDocDirPath atomically:YES];
appDelegate.counter ++;
[[NSUserDefaults standardUserDefaults] setInteger:appDelegate.counter ForKey:#"ImageNumber"];
//add in database with fileName
Retrieve like this:
appDelegate = (appDelegate *)[[UIApplication sharedApplication] delegate];
NSString *strDocDirPath = [appDelegate applicationDocumentsDirectory];
strDocDirPath = [strDocDirPath stringByAppendingPathComponent:#"Image001.png"]; //use your database image name here
UIImage *img = [[UIImage alloc]initWithContentsOfFile:strDocDirPath];
Every time you can Create a Random String and Rename that image Which you capture to Randomly generated string.
NSString *letters = #"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
NSMutableString *randomString = [NSMutableString stringWithCapacity:6];
for (int i=0; i<6; i++)
{
[randomString appendFormat: #"%C", [letters characterAtIndex: arc4random() % [letters length]]];
}
NSString *fileName = [NSString stringWithFormat:#"%#.png",randomString];
Hope this will Help.

How to search a word from the .txt file that has been included in the project in xcode

I have included a .txt file in my project and i have to find a keyword from that file by entering the keyword in the search bar. Help me with the piece of code for searching the keyword that is present in the file.
Try that
//Get the contents of the file
NSString *contentString = [NSString stringWithContentOfFile:<i>path/to/your/file.txt</i> encoding:<i>textEncoding<i> error:<i>&error</i>];
if (!error) {
NSRange range = [contentString rangeOfString:<i>yourKeyword</i>];
if (theRange.location != NSNotFound)
//do whatever you want
}
NB : this will work as long as your text file is not "too big". If you have to work with bigger files, take a look at NSInputStream to only parse chunks of the file
Try this:
-(void)searchWord:(NSString *)wordToBeSearched{
NSString* path = [[NSBundle mainBundle] pathForResource:#"wordFile"
ofType:#"txt"];
NSString* content = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:NULL];
NSString *delimiter = #"\n";
NSArray *items = [content componentsSeparatedByString:delimiter];
NSString *character = #" ";
BOOL isContain = NO;
for (NSString *wordToBeSearched in items) {
isContain = ([string rangeOfString:wordToBeSearched].location != NSNotFound);
}
return isContain;
}
You can go here for more details

Problem with CGImageDestination and file naming

I am capturing images from the camera, using AVCapture as I have need of speed and the standard kit stuff is way too slow.
I have problem whereby the file that is being output (an animated GIF) is having it's file name mangles by the CGImageDestination functions...
When I output the NSURL (cast to a CFURLRef) to the log I get the path/filename I intended:
2011-09-04 20:40:25.914 Mover[3558:707] Path as string:.../Documents/91B2C5E8-F925-47F3-B539-15185F640828-3558-000003327A227485.gif
However, once the file is created and saved it actually lists the filename as this:
2011-09-04 20:40:25.960 Mover[3558:707] file: .91B2C5E8-F925-47F3-B539-15185F640828-3558-000003327A227485.gif-TtNT
See the difference? the period at the start and the 4 character suffix?
Whats really wierd is that it doesn't always do it, about 40% of the time it works OK. However it's preventing the code working further down the line where I'm listing them with previews in a table view.
Does anyone know why and how to stop it doing this?
Here's the code:
- (void)exportAnimatedGif{
NSString *guidPath = [[NSProcessInfo processInfo] globallyUniqueString];
NSString *tmpPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:guidPath];
NSString *path = [tmpPath stringByAppendingPathExtension:#"gif"];
NSLog(#"Path as string:%#", path);
CGImageDestinationRef destination = CGImageDestinationCreateWithURL((CFURLRef)[NSURL fileURLWithPath:path], kUTTypeGIF, [captureArray count], NULL);
NSDictionary *frameProperties = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:testDisplay3.displayValue] forKey:(NSString *)kCGImagePropertyGIFDelayTime]
forKey:(NSString *)kCGImagePropertyGIFDictionary];
NSDictionary *gifProperties = [NSDictionary dictionaryWithObject:
[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:2], (NSString *)kCGImagePropertyGIFLoopCount,
[NSNumber numberWithFloat:testDisplay3.displayValue], (NSString*)kCGImagePropertyGIFDelayTime,
[NSNumber numberWithFloat:testDisplay3.displayValue], (NSString*)kCGImagePropertyGIFUnclampedDelayTime,
nil]
forKey:(NSString *)kCGImagePropertyGIFDictionary];
for (int ii = 0; ii < [captureArray count]; ii++)
{
UIImage *tmpImg = [[UIImage alloc] init];
tmpImg = [captureArray objectAtIndex:ii];
CGImageDestinationAddImage(destination, tmpImg.CGImage, (CFDictionaryRef)frameProperties);
}
CGImageDestinationSetProperties(destination, (CFDictionaryRef)gifProperties);
CGImageDestinationFinalize(destination);
CFRelease(destination);
//TEST OUTPUT GENERATED FILES
NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] error:nil];
for (int xx = 0; xx < [contents count]; xx++)
{
NSLog(#"file: %#", [contents objectAtIndex:xx]);
}
//END TEST CODE
[captureArray removeAllObjects];
}
AFAIK this is a temporary file that CGImageDestinationFinalize makes, the reason you see them is that CGImageDestinationFinalize failed. I think that if you check the file sizes you'll see that the ones with mangled names have a file size of 0.
I started check for succes after I got these files :)
bool success = CGImageDestinationFinalize(destination);
CFRelease(destination);
if (success) {
NSLog(#"animated GIF file created at %#", path);
} else {
NSLog(#"failed to create gif at %#", path);
}