0

我正在開發我的學校項目,我選擇了構建使用相機的應用程序。我已經設法使用它並將其附加到UIImageView中,但是當應用程序被殺時,我很難使圖像永久存在。Objective-C:當應用程序被殺時保存圖像

我的意思是,我希望圖像保留在視圖中,當我殺死應用程序並再次打開它時,因爲在我當前的項目中,當我殺死應用程序時,圖像也消失了,當我打開應用程序再次。

這裏是我的代碼:

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; 
    insertPhoto1.contentMode=UIViewContentModeCenter; 
    [insertPhoto1 setImage:image]; 
    [self dismissModalViewControllerAnimated:YES]; 
} 

這對於分流:

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; 
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 
      { 
       [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera]; 
      } 
      else 
      { 
       [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; 
      } 
      [imagePicker setDelegate:self]; 
      [self presentModalViewController:imagePicker animated:YES]; 
+0

首先嚐試檢查碰撞發生的位置。 – sairam

回答

1

可以保存並從磁盤檢索圖像,像這樣:

保存圖像:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; 

    NSData* imageData = UIImageJPEGRepresentation(image, 1.0); 
    NSString* imagePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"/photo1.png"]; 
    [imageData writeToFile:imagePath atomically:YES]; 

    insertPhoto1.contentMode = UIViewContentModeCenter; 
    [insertPhoto1 setImage:image]; 
    [self dismissModalViewControllerAnimated:YES]; 
} 

Retrievin克的UIViewController中viewWillAppear中的圖像:

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    NSString* imagePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"/photo1.png"]; 
    UIImage* imageFromFileSystem = [UIImage imageWithContentsOfFile:imagePath]; 

    UIImageView imageView = [[UIImageView alloc] initWithImage:imageFromFileSystem]; 
    [self.view addSubView:imageView]; 
} 
+0

我應該在哪裏放?我是小白,抱歉。 –

+0

我嘗試過,但仍然無法正常工作。 –

+0

viewWillAppear中的固定代碼,因爲我的原始答案中的文件名錯誤(複製和粘貼錯誤) – Brendt

0

一個選項是在保存NSUserDefaults的照片時,應用程序被殺害。 保存圖像:

UIImage* image = [UIImage imageNamed:@"example_image.png"]; 
NSData* imageData = UIImageJPEGRepresentation(image, 1.0); 
NSString* imagePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"/saved_example_image.png"]; 
[imageData writeToFile:imagePath atomically:YES]; 

然後就可以調用圖像時,您的應用程序被再次啓動。 正在檢索圖片:

NSString* imagePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"/saved_example_image.png"]; 
UIImage* imageFromFileSystem = [UIImage imageWithContentsOfFile:imagePath]; 
相關問題