Jac*_*ins 1 iphone objective-c uiimageview ios nsdocumentdirectory
我正在使用这段代码尝试将图像保存到文档目录:
//FOR TESTING ON SIMULATOR
UIImage* image = [UIImage imageNamed:@"ReceiptImage1.jpg"];
NSData *imageData = UIImagePNGRepresentation(image);
NSString* imageName = @"Receipt1Image1.png";
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
[imageData writeToFile:fullPathToFile atomically:NO];
self.receiptImage1 = fullPathToFile;
self.receiptImageView1.image = [UIImage imageWithContentsOfFile:fullPathToFile];
NSLog(@"fullPathToFile %@", fullPathToFile);
Run Code Online (Sandbox Code Playgroud)
但是,这段代码没有将imageViews图像设置为我正在尝试写入文档目录的收据图像.
有人可以解释我做错了什么以及如何解决它?
谢谢,
插口
用这个:
- (void)saveImage:(UIImage*)image:(NSString*)imageName{
NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format.
NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it
NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]]; //add our image to the path
[fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the path (image)
NSLog(@"image saved");}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4390 次 |
| 最近记录: |