0

我可以从图库中选择图像并将其放置在 ImageView 中,但稍后在更改 viewController 并返回图像时我无法将其固定到位并消失

-(IBAction)LibraryPicture
{
if([UIImagePickerController isSourceTypeAvailable:
    UIImagePickerControllerSourceTypePhotoLibrary])
{
    UIImagePickerController *picker= [[UIImagePickerController  alloc]init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentModalViewController:picker animated:YES];

  }
  NSString  *pngPath = [NSHomeDirectory()  stringByAppendingPathComponent:@"Documents/Test.png"];
NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.jpg"];


                }

 -(void)imagePickerController:(UIImagePickerController *)picker
  didFinishPickingImage : (UIImage *)image
             editingInfo:(NSDictionary *)editingInfo
{
 imageSelect.image = image;
 [picker dismissModalViewControllerAnimated:YES];

}


 -(void)imagePickerControllerDidCancel:(UIImagePickerController *)  picker
 {
[picker dismissModalViewControllerAnimated:YES];
 }

- (BOOL)shouldAutorotateToInterfaceOrientation:     (UIInterfaceOrientation)interfaceOrientation
 {

 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 - (IBAction)saveImage:(id)sender {

  NSLog(@"save image & Keep");
  }

此图像应保留到用户再次更改它

4

1 回答 1

0

它发生的原因是因为您没有保存图像。在您保存图像的 IBAction 中,您什么也没做。因此,您需要保存图像,然后在 viewdidload 中(当用户回到 VC 时),加载该图像。

要保存它,您需要执行以下操作:

NSString *imgName= @"imgname.png";
[[NSUserDefaults standardUserDefaults]setValue:imgName forKey:@"imageName"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:imgName];
UIImage *imageSave = image; // The imageView that you want to save
NSData *imageData = UIImagePNGRepresentation(imageSave);
[imageData writeToFile:savedImagePath atomically:NO];

并且要在您回到该 VC 时重新加载图像,请在您的 viewDidload 中执行以下操作:

NSString *imgName= [[NSUserDefaults standardUserDefaults]valueForKey:@"imageName"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *filePath=[NSString stringWithFormat:@"%@/%@",documentsDir,imgName];
[BackGround setImage:[UIImage imageWithContentsOfFile:filePath]];
于 2015-08-09T09:15:42.133 回答