IOS - save image in jpg format

My question is what format the image saves if it is dat or jpg. This is the code 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];
        }
+4
source share
2 answers

Below is a snippet of code to save a .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];
+3
source

You have to use

stringByAppendingPathComponent method for creating or getting the exact valid path

Use this method:

    NSString * jpegPath = [_folderPath stringByAppendingPathComponent:imageName];// [NSString stringWithFormat:@"%@%@",_folderPath,imageName];
0
source

Source: https://habr.com/ru/post/1526166/


All Articles