2012-04-05 6 views
0

NSMutableArrayに画像を保存しましたが、現在はviewDidLoadに表示させようとしています。 initWithContentsOfFileを呼び出そうとしましたが、うまくいかないようです。これは、コードがどのように見えるかです: imageView.image = [[UIImage alloc] initWithContentsOfFile:[self.array objectAtIndex:0]];-initWithContentsOfFile:NSMutableArrayの場合

私は私が保存された画像の負荷を持っている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]; 

    } 

EDIT:これは私が、画像にタグを付けるとそのタグに基づいてそれらを削除しています方法です:

- (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"]; 
} 
+0

クラッシュしますか?報告されたエラーは何ですか? – joerick

+0

@joerickいいえ、私はクラッシュしません。しかし、(マルチタスクバーでも)アプリケーションを終了してアプリケーションを再起動すると、イメージは正しく保存/ロードされません。私はinitWithContentsOfFileが原因だと感じています。 – John

+0

実際に何が配列に格納されていますか?通り? URL?そのインスタンスだけをUIImageインスタンスを格納する場合は、initWithContentsOfFileを使用することはできません... – Daniel

答えて

2

問題は、イメージをプロパティのリストに保存できないことです。これは、ユーザーのデフォルトでイメージを保存するときに実行しようとしていることです。イメージをNSDataオブジェクトに変換するには、アーカイバを使用する必要があります。

+0

私は以前これをやっていましたが、画像にタグを付ける必要があります。 NSDataには、確認できるタグプロパティはありません。 – John

+0

どのように画像にタグを付けましたか?私はあなたのコードでそれについて何も見たことがありませんでした。 – rdelmar

+0

私はインターフェースビルダーでタグ付けしています(ユーザーが希望する場合に画像を削除できるようにするため)。私はちょうど私がそれを行うために使用するコードで私のOPを更新しました。 – John

0

あなたがいるように見えます有効なイメージパスを初期化メソッドに渡しません。パスが正しいこと、および画像の拡張子が含まれていることを確認してください。 のimageプロパティは、設定したときに画像が保持されるため、実際にはinitWithContentsOfFile:を呼び出すべきではありません。UIImageView通常、メモリリークが発生します(自動参照カウントを使用している場合を除きます)。 imageNamed:のような静的イニシャライザの代わりに、システムキャッシュを使用するボーナスが追加され、デバイスの特性に基づいてイメージの正しいバージョンが自動的にロードされます(たとえば、より高解像度のバリアントデバイスが網膜ディスプレイを有する場合には画像の)。