Reputation: 9075
I am trying to save uiimage to the document folder, but can't find it work with following code:
-(void) BtnClicked: (id)sender{
NSLog(@"Btn clicked");
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"file2.txt"];
NSString *pngPath = [documentsDirectory stringByAppendingString:@"test2.png"];
NSString *str = @"hello world";
UIImage *uiImage = [UIImage imageNamed:@"test.png"];
NSData *dataImage = [NSData dataWithData:UIImagePNGRepresentation(uiImage)];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 300, 300)];
imageView.image = uiImage;
[self.view addSubview: imageView];
[dataImage writeToFile:pngPath atomically:YES];
[str writeToFile:filePath atomically:TRUE encoding:NSUTF8StringEncoding error:NULL];
}
As the code showing above, I add a string to test together, I could find the file2.txt
in the container download by Xcode but can't find the png
file, I also add a UIImage
view to show the image to avoid other error, and the image was successfully showed.
What has been wrong?
Upvotes: 1
Views: 131
Reputation: 16466
You have a small mistake
observe both
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"file2.txt"];
NSString *pngPath = [documentsDirectory stringByAppendingString:@"test2.png"];
stringByAppendingPathComponent
should be used at the place of stringByAppendingString
Hope it will solve your problem
EDIT
Instead of use like this you should create a class which allows to do all operation with document folder
I already posted answer in https://stackoverflow.com/a/45963431/4601900
Please use this class if it is relevant to
Upvotes: 1