Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - save image in jpg format

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];
        }
like image 273
Rodrigo Avatar asked Oct 01 '22 15:10

Rodrigo


1 Answers

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];
like image 178
iPatel Avatar answered Oct 13 '22 10:10

iPatel