6

My question is what format the image is saved, if is dat or jpg. This is the code that i used:

    NSString * urlImage = .....;
    NSString * _folderPath = .....;

    NSString * imageName = [[urlImage componentsSeparatedByString:@"/"] lastObject];
        NSString * jpegPath = [NSString stringWithFormat:@"%@%@",_folderPath,imageName];

        if (![[NSFileManager defaultManager] fileExistsAtPath:jpegPath])
        {
            NSURL *url = [NSURL URLWithString:urlImage];
            //Download image
            UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:url]];

            //Save image
            NSData *data = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];//1.0f = 100% quality
            [data writeToFile:jpegPath atomically:YES];
        }
iPatel
  • 46,010
  • 16
  • 115
  • 137
Rodrigo
  • 123
  • 1
  • 6

2 Answers2

4

Following is piece of code for save .jpg image

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *docs = [paths objectAtIndex:0];
NSString* path =  [docs stringByAppendingFormat:@"/image1.jpg"];

NSData* imageData = [NSData dataWithData:UIImageJPEGRepresentation(imageView.image, 80)];
NSError *writeError = nil;
[imageData writeToFile:path options:NSDataWritingAtomic error:&writeError];
iPatel
  • 46,010
  • 16
  • 115
  • 137
  • Thanks for you answer. One question, the file is saved in .dat or .jpg? and is necessary to retrieve with a NSData? – Rodrigo Feb 10 '14 at 15:05
  • @RodDL - for get image from document directory http://stackoverflow.com/questions/6663511/iphone-how-to-display-document-directory-images-in-image-view – iPatel Feb 11 '14 at 05:07
  • how can I change resolution of photo while saving yo JPG? – Radek Wilczak Dec 04 '17 at 13:21
0

You should use

stringByAppendingPathComponent method to create or get exact valid path

Use this way:

    NSString * jpegPath = [_folderPath stringByAppendingPathComponent:imageName];// [NSString stringWithFormat:@"%@%@",_folderPath,imageName];
Bhumeshwer katre
  • 4,671
  • 2
  • 19
  • 29