0

私は学校のプロジェクトの開発に取り組んでおり、カメラを使用するアプリを構築することにしました。私はなんとかそれを使用してにアタッチしましUIImageViewたが、アプリが強制終了されたときにそのイメージを永続的にするのに苦労しています。

つまり、アプリを強制終了して再度開いたときに、添付したビューに画像を残しておきたいのです。現在のプロジェクトでは、アプリを強制終了したときに、アプリを再度開いたときに画像も消えてしまうからです。

これが私のコードです:

- (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];
4

2 に答える 2

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];
}

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];
}
于 2012-09-20T07:01:33.290 に答える
0

1 つのオプションは、アプリが強制終了されたときに写真を 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];
于 2012-09-20T07:07:31.247 に答える