0

画像を NSMutableArray に保存しましたが、今はそれらを viewDidLoad に表示させようとしています。initWithContentsOfFile を呼び出してみましたが、うまくいかないようです。コードは次のようになります。 imageView.image = [[UIImage alloc] initWithContentsOfFile:[self.array objectAtIndex:0]];

保存した画像を読み込むために initWithContentsOfFile の代わりに何を使用すればよいかわかりません。ユーザーのデフォルトを使用して plist に画像を保存できるかどうかさえわかりません。私はしばらくの間それを研究してきましたが、役に立ちませんでした。どんな助けでも大歓迎です、ありがとう!

編集:ここに追加のコードがあります:

- (IBAction)grabImage {
    self.imgPicker = [[UIImagePickerController alloc] init];
    self.imgPicker.delegate = self;
    self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        _popover = [[UIPopoverController alloc] initWithContentViewController:imgPicker];
        [_popover presentPopoverFromRect:self.imageView.bounds inView:self.imageView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    } 

    else {
        [self presentModalViewController:imgPicker animated:YES];
    }
    [self.imgPicker resignFirstResponder];
}
// Sets the image in the UIImageView
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
    if (imageView.image == nil) {
        imageView.image = img;

        [self.array addObject:imageView.image];

        [picker dismissModalViewControllerAnimated:YES];
        [self.popover dismissPopoverAnimated:YES];
        return;

    }

    if (imageView2.image == nil) {
        imageView2.image = img;
        NSLog(@"The image is a %@", imageView);
        [self.array addObject:imageView2.image];

        [picker dismissModalViewControllerAnimated:YES];
        [self.popover dismissPopoverAnimated:YES];
        return;
    }

    if (imageView3.image == nil) {
        imageView3.image = img;

        [self.array addObject:imageView3.image];

        [picker dismissModalViewControllerAnimated:YES];
        [self.popover dismissPopoverAnimated:YES];
        return;
    }
}

- (void)applicationDidEnterBackground:(UIApplication*)application {
    NSLog(@"Image on didenterbackground: %@", imageView);

   [self.array addObject:imageView.image];
    [self.array addObject:imageView2.image];
     [self.array addObject:imageView3.image];


            [self.user setObject:self.array forKey:@"images"];
    [user synchronize];

            }

- (void)viewDidLoad
    {
        self.user = [NSUserDefaults standardUserDefaults];
        NSLog(@"It is %@", self.user);
        self.array = [[self.user objectForKey:@"images"]mutableCopy];
        imageView.image = [[UIImage alloc] initWithContentsOfFile:[self.array objectAtIndex:0]];
        imageView2.image = [[UIImage alloc] initWithContentsOfFile:[self.array objectAtIndex:1]];
        imageView3.image = [[UIImage alloc] initWithContentsOfFile:[self.array objectAtIndex:2]];




        UIApplication *app = [UIApplication sharedApplication];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(applicationDidEnterBackground:)
                                                     name:UIApplicationDidEnterBackgroundNotification
                                                   object:app];

        backToGalleryButton.hidden = YES;
        tapToDeleteLabel.hidden = YES;
        deleteButton1.hidden = YES;
        [super viewDidLoad];

    }

編集:これは、画像にタグを付けて、タグに基づいて削除する方法です:

- (IBAction)deleteButtonPressed:(id)sender {
    NSLog(@"Sender is %@", sender);
    UIAlertView *deleteAlertView = [[UIAlertView alloc] initWithTitle:@"Delete"
                                                              message:@"Are you sure you want to delete this photo?"
                                                             delegate:self
                                                    cancelButtonTitle:@"No"
                                                    otherButtonTitles:@"Yes", nil];
    [deleteAlertView show];

    int imageIndex = ((UIButton *)sender).tag;
    deleteAlertView.tag = imageIndex;
}

- (UIImageView *)viewForTag:(NSInteger)tag {
    UIImageView *found = nil;
    for (UIImageView *view in self.array) {
        if (tag == view.tag) {
            found = view;
            break;
        }
    }
    return found;
}

- (void)alertView: (UIAlertView *) alertView 
clickedButtonAtIndex: (NSInteger) buttonIndex
{


    if (buttonIndex != [alertView cancelButtonIndex]) {
        NSLog(@"User Clicked Yes. Deleting index %d of %d", alertView.tag, [array count]);
        NSLog(@"The tag is %i", alertView.tag);

        UIImageView *view = [self viewForTag:alertView.tag];
        if (view) {
            [self.array removeObject:view];
        }

        NSLog(@"After deleting item, array count  = %d", [array count]);
    NSLog(@"Returned view is :%@, in view: %@", [self.view viewWithTag:alertView.tag], self.view);
        ((UIImageView *)[self.view viewWithTag:alertView.tag]).image =nil;
    }

    [self.user setObject:self.array forKey:@"images"];
}
4

2 に答える 2

2

問題は、画像をプロパティ リストに保存できないことです。これは、ユーザーの既定値に画像を保存するときにしようとしていることです。アーカイバーを使用して、画像を保存可能な NSData オブジェクトに変換する必要があります。

于 2012-04-06T00:37:14.043 に答える
0

初期化メソッドに有効な画像パスを渡していないようです。パスが正しいこと、および画像拡張子が含まれていることを確認してください。

ただし、のプロパティは設定時にイメージを保持するinitWithContentsOfFile:ため、この場合は呼び出すべきではありません。これは通常、メモリ リークにつながります (自動参照カウントを使用していない場合)。代わりに などの静的イニシャライザの 1 つを使用してください。これには、システム キャッシュを使用し、デバイスの特性に基づいて正しいバージョンのイメージを自動的にロードするという追加のボーナスがあります (たとえば、デバイスに Retina ディスプレイがある場合は画像)。UIImageViewimageimageNamed:

于 2012-04-06T00:03:11.150 に答える