2011-10-04 12 views
5

Tôi đang phát triển ứng dụng này trên iPad.Hiển thị ảnh từ NSDocumentDirectory

Các mã này cho nút 'Duyệt' cho phép người dùng xem ảnh từ thư viện của 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]; 
} 

Khi ảnh được chọn, ảnh sẽ được lưu vào ứng dụng bằng 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]; 
} 

Bây giờ tôi phải bao gồm nút 'Hiển thị' trên màn hình đầu tiên. Khi khai thác trên nút, nó sẽ hiển thị một giao diện mới (bộ điều khiển xem phương thức) và hiển thị tất cả các hình ảnh trong hình thu nhỏ/tableview từ NSDocumentDirectory.

Khi ảnh được chọn, ảnh sẽ bị xóa khỏi NSDocumentDirectory.

Tôi làm cách nào để thực hiện việc này?

+0

Ai đó giúp tôi! – Lloydworth

Trả lời

2

Trước hết, trong khi lưu trữ hình ảnh trong thư mục Tài liệu, hãy thử lưu trữ chúng với quy ước đặt tên như giữ biến số lượt truy cập và đặt tên hình ảnh của bạn theo nó. Một số điều như thế này:

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

Bây giờ, khi bạn đã tất cả các hình ảnh được lưu trữ trong thư mục tài liệu và muốn có được tất cả trong số họ, bạn có thể nhận được chúng bằng cách viết mã này:

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

Khi bạn' có tất cả hình ảnh bạn có thể hiển thị chúng dưới dạng hình thu nhỏ và sau đó khi bạn muốn xóa hình ảnh, hãy sử dụng phương pháp này:

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

Tôi hy vọng điều này sẽ giúp bạn.

Để điền bảng bằng hình ảnh, bạn sẽ cần một ô tùy chỉnh. Bạn có thể tham khảo hướng dẫn của Apple được gọi là AdvancedTableViewCells. Nó sẽ cho bạn thấy làm thế nào bạn có thể cư trú bảng của bạn với hình ảnh. Họ có một hình thu nhỏ chính trong mỗi hàng. Bạn phải tùy chỉnh nó và làm cho nó 3 hoặc 4 hình thu nhỏ theo yêu cầu của bạn. Khác hơn là loại bỏ tất cả mọi thứ từ ô tùy chỉnh đó.

+0

Cảm ơn! Nhưng bạn có bất kỳ ý tưởng làm thế nào để hiển thị các mảng hình ảnh vào một cái nhìn mới với hình thu nhỏ? – Lloydworth

+0

Lấy bàn. Làm cho ô tùy chỉnh của nó chứa 3 trong số 4 hình ảnh trong một hàng bất cứ điều gì thuận tiện cho bạn. Điền các hình ảnh từ mảng hình ảnh mà chúng tôi có và bạn sẽ có tất cả hình ảnh được hiển thị. Bạn cũng cần một nút cho mỗi hình ảnh (một hình chữ thập nhỏ ở góc trên cùng bên phải của mỗi hình ảnh) trong ô tùy chỉnh để xóa hình ảnh. –

+0

Đừng bận tâm khi đưa ra một ví dụ về cách điền bảng với mảng? – Lloydworth