2011-10-04 14 views
5

私はiPadでこのアプリケーションを開発しています。NSDocumentDirectoryから写真を表示

「参照」ボタンのこれらのコードは、ユーザーがiPadのギャラリーから写真を見ることを可能にします。

- (IBAction) BrowsePhoto:(id)sender 
{ 
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; 
imagePickerController.delegate = self; 
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:imagePickerController]; 
[popover setPopoverContentSize:CGSizeMake(320,320)]; 
[popover presentPopoverFromRect:CGRectMake(200,200,-100,-100) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 
self.popoverController = popover; 
[imagePickerController release]; 
} 

写真が選択されると、写真はNSDocumentDirectoryを使用してアプリケーションに保存されます。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)selectedImage editingInfo:(NSDictionary *)editingInfo 
{ 
[self.popoverController dismissPopoverAnimated:YES]; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDir = [paths objectAtIndex:0]; 
NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:@"SavedImage.png"]; 
UIImage *image = imageView.image; 
NSData *imageData = UIImagePNGRepresentation(image); 
[imageData writeToFile:savedImagePath atomically:NO]; 
} 

ここで、最初の画面に[表示]ボタンを追加する必要があります。 ボタンをタップすると、新しいビュー(モーダルビューコントローラ)が表示され、すべての写真がNSDocumentDirectoryのサムネイル/テーブルビューに表示されます。

写真を選択すると、写真はNSDocumentDirectoryから削除されます。

どうすればいいですか?

+0

誰かが私を助けてください! – Lloydworth

答えて

2

まず、画像をDocumentsフォルダに保存しながら、カウンタ変数を保持するなどの命名規則で画像を保存し、それに応じて画像名を付けてみてください。このようないくつかの事:あなた一度

-(NSMutableArray *)GetImage:(NSMutableArray *)arrayImgNames 
{ 
     NSMutableArray *tempArray; 
      for(int i=0;i<[arrayImgNames count]; i++) 
    { 
     NSArray *paths1 = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths1 objectAtIndex:0]; 
     NSString *filePath = [documentsDirectory stringByAppendingPathComponent: [arrayImgNames objectAtIndex:i]]; 
     [tempArray addObject:[[UIImage alloc] initWithContentsOfFile:filePath]]; 
     return tempArray; 
    } 
} 

」:あなたはドキュメントフォルダに保存されているすべての画像をきたし、それらのすべてを取得したい一度今

NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png", counter]]; 

は、あなたがこのコードを書くことでそれらを得ることができますあなたがサムネイルとして表示することができ、すべての画像を持って、削除したいときに画像がこの方法を使用veは:私はこれがあなたの役に立てば幸い

-(int)removeImage:(NSString*)FileName1 
{ 
    NSFileManager *filemanager=[NSFileManager defaultManager]; 
    NSArray *path1=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); 
    NSString *documentdirectory = [path1 objectAtIndex:0]; 
    NSString *filepath=[documentdirectory stringByAppendingPathComponent:FileName1]; 
    if([filemanager fileExistsAtPath:filepath]==TRUE) 
    { 
     [filemanager removeItemAtPath:filepath error:nil]; 
    } 
    return 0; 
} 

を。

イメージにテーブルを挿入するには、カスタムセルが必要です。アップルのAdvancedTableViewCellsというチュートリアルを参照することができます。どのようにしてテーブルにイメージを埋め込むことができるかを示します。彼らはすべての行にメインサムネイルを持っています。あなたはそれをカスタマイズし、必要に応じて3つまたは4つのサムネイルにする必要があります。それ以外は、そのカスタムセルからすべてを削除します。

+0

ありがとう!しかし、画像の配列をサムネイルで新しいビューに表示する方法はありますか? – Lloydworth

+0

テーブルを取る。あなたの都合の良いものを1列に4つの画像のうち3つを含むカスタムセルにします。私たちが持っている画像の配列から画像を塗りつぶすと、すべての画像が表示されます。画像を削除するには、カスタムセル内のすべての画像(各画像の右上隅にある小さな十字形)のボタンも必要です。 –

+0

テーブルに配列を設定する方法の例を教えても構いませんか? – Lloydworth

関連する問題