Alex
Alex

Reputation: 11137

How can i upload a UIImage to a specific folder in Documents?

I have a image name (lets say @"image.jpg" ) and i would like to save it to a folder i have created in my Documents folder named "coffeeShops". How can i do so ?

Upvotes: 0

Views: 72

Answers (1)

ySgPjx
ySgPjx

Reputation: 10245

NSString *docs = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];
NSString *dir = @"coffeeShops";
NSString *destPath = [docs stringByAppendingPathComponent:dir];

// check if the destination directory exists, if not create it
BOOL isDirectory;
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:destPath isDirectory:&isDirectory];
if(!exists || !isDirectory) {
    NSError *error = nil;
    [[NSFileManager defaultManager] createDirectoryAtPath:destPath withIntermediateDirectories:NO attributes:nil error:&error];
    if(error != nil) {
        // should do error checking here
        NSLog(@"%@",[error localizedDescription]);
    }
}

NSString *fileName = @"image.jpg";
NSString *path = [destPath stringByAppendingPathComponent:fileName];

[UIImageJPEGRepresentation(image) writeToFile:path atomically:YES];

Upvotes: 2

Related Questions