1

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];
Fabio Berger
  • 1,921
  • 2
  • 24
  • 29
Irrd
  • 325
  • 1
  • 6
  • 18

1 Answers1

1

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.

Community
  • 1
  • 1
Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
  • What I have done sofar is first I take the image name from the asset library. Then I want to search the real path of it. see my updated question.. Now I want to read this actual file using NSInputStream. – Irrd Aug 31 '16 at 07:24
  • IS it correct the way I have done,, and can I read the actual file using this path using NSInputStream? – Irrd Aug 31 '16 at 07:25
  • what you want to achieve ? tell me requirement – Ketan Parmar Aug 31 '16 at 07:36
  • I want to upload user selected image to my server. Server needs a byte array of my UIImage.I encoded to base64 and send to the server. But my data doesnt accept by the server. If I encoded it with an online tool and send then it qccept,, also I checked the same image for both iOS and Android but base64 string is different. But Android one is accept by the server. So I checked how android one get the byte array from the image. It has used INputstream to get the byte data.like this public static byte[] readBytesFromFile(File file) throws IOException {InputStream is = new FileInputStream(file); – Irrd Aug 31 '16 at 07:42
  • So I need to do the same thing in iOS too – Irrd Aug 31 '16 at 07:43
  • have you tried something like, `NSString *byteArray = [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];` ? If image is jpeg then jpegrepresentation!! – Ketan Parmar Aug 31 '16 at 07:46
  • have you tried normal way to send image to server as normal data (i.e. in multipart form data) ? – Ketan Parmar Aug 31 '16 at 07:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122261/discussion-between-irrd-and-ketan-parmar). – Irrd Aug 31 '16 at 07:57