How to search an Image file

177 Views Asked by At

Is there anyway to check whether a particular image file exists in my iPhone. Lets say my image name is IMG_123.JPG. I want to search this image and if it exists I want to create a NSInputStream object using that File. If the image is not in NSBundle it can be saved in photo library or somewhere.

Please help me. Thanks

UPDATE

NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [pathArray objectAtIndex:0];
NSString* dataPath = [documentsDirectory  stringByAppendingPathComponent:@"IMG_2594.JPG"];

UPDATE 2

Getting the image when user click the picker image

copyOfOriginalImage = [UIImage imageWithCGImage:[[imageAsset defaultRepresentation] fullResolutionImage]];

Convert the image into NSData

imageData = UIImageJPEGRepresentation(copyOfOriginalImage, 1.0);

Then I convert it into base64

strEncoded=[imageData base64EncodedStringWithOptions:0];
1

There are 1 best solutions below

8
Ketan Parmar On

Your image can be at documentdirectory or assets or appbundle in your application.

Images which are located in photolibrary, you can't called it part of your app's database.

So, you can check that your image is available in documentdirectory or any directory like tempdirectory or customdirectory if you have made like,

 NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSFileManager *manager = [NSFileManager defaultManager];

NSArray *filesIndir = [manager contentsOfDirectoryAtPath:path error:nil];

if ([filesIndir containsObject:@"IMG_123.JPG"]) {

    NSLog(@"document directory contains : IMG_123.JPG");
}

It will check directly in document directory. If you are making custom directory in document directory then provide path of it.

Now If your image is in app bundle then you can check it like,

  UIImage *image = [UIImage imageNamed:@"IMG_123.JPG"];

if (image) {

    NSLog(@"yes image available in app bundle");
}

If your image is in assests then you can check it like,

   UIImage *image2 = [UIImage imageNamed:@"IMG_123"];

if (image2) {

    NSLog(@"yes image available in assets");
}

Now, for checking image is available in photo library, This is a bit critical task that you should have the list of all images and then you can check that it available or not. For that you have to use ALAssetsLibrary.

Refer This SO answer for managing this.

You can refer this so post also.