Vinay
Vinay

Reputation: 171

How NSDocumentsDirectory works?

I'm using NSDocumentDirectory to save an image each time user may save different different edited image in my application on click of an button like below

-(void)saveImg{
    NSData *pngData = UIImagePNGRepresentation(signatureImage);
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
        NSString *documentsPath = [paths objectAtIndex:0]; 
        NSString *filePath = [documentsPath stringByAppendingPathComponent:@"myPhoto.png"]; 
        [pngData writeToFile:filePath atomically:YES]; 
}

and i'm getting my image in another class like

-(void)getMyImg{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
    NSString *documentsPath = [paths objectAtIndex:0]; 
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"myPhoto.png"]; 
    NSData *pngData = [NSData dataWithContentsOfFile:filePath];
    UIImage *myImage = [UIImage imageWithData:pngData];
    imgView.image = myImage;
}

My question is using Apps Documents Directory will cause any memory issues?, like say if i click my saveImg method more than 100 times then is there 100 image set inside my apps DocumentDirectory or on every call the previously saved image is replaced by the new one(since i'm giving same name as myPhoto.png), how exactly the documentsDirectory works?

Any help is appreciated in advance.

Upvotes: 1

Views: 733

Answers (2)

borrrden
borrrden

Reputation: 33423

The default behavior is to overwrite the file if it has permission (which is does in the documents directory).

Upvotes: 1

Janak Nirmal
Janak Nirmal

Reputation: 22726

Memory Leak: Answer is NO. There is no memory leak as all objects will be autoreleased.

Image will be Replaced Answer is YES. Image will be replaced.

Hope this helps.

Upvotes: 3

Related Questions