5

私はiPadでこのアプリケーションを開発しています。

これらの「参照」ボタンのコードにより、ユーザーは 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 から削除されます。

これどうやってするの?

4

1 に答える 1

2

まず、Documents フォルダーに画像を保存する際に、カウンター変数を保持するなどの命名規則に従って保存し、それに従って画像に名前を付けます。このようなもの:

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

すべての画像をドキュメント フォルダーに保存し、それらすべてを取得したい場合は、次のコードを記述して取得できます。

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

すべての画像を取得したら、それらをサムネイルとして表示できます。画像を削除する場合は、次の方法を使用します。

 -(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と呼ばれる Apple のチュートリアルを参照できます。テーブルに画像を入力する方法を示します。すべての行にメインのサムネイルがあります。カスタマイズして、要件に応じて 3 つまたは 4 つのサムネイルにする必要があります。それ以外は、そのカスタムセルからすべてを削除します。

于 2011-10-04T06:20:24.820 に答える