2011-07-12 44 views
10

Trong một hình ảnh ứng dụng được lưu trữ tại văn bản thư mục Folder,Iphone: Cách hiển thị hình ảnh thư mục tài liệu trong chế độ xem hình ảnh?

tôi muốn hiển thị hình ảnh đặc biệt trong quan điểm của hình ảnh,

Làm thế nào mà hình ảnh hiển thị trong hình ảnh Xem từ danh mục tài liệu?

+1

Câu trả lời duy nhất tôi có thể nghĩ đến ở đây, là '[[UIImageView alloc] initWithImage: [UIImage imageNamed: @" my_image "]]'. Tại sao điều này không hiệu quả với bạn? –

+1

tôi đang tải xuống hình ảnh từ máy chủ. hình ảnh được lưu trong Thư mục Tài liệu. Tôi muốn hiển thị hình ảnh đó ở chế độ xem hình ảnh –

+0

Xin chào Ankit. Vui lòng kiểm tra câu trả lời của tôi. Tôi cũng đã cung cấp mã mẫu. Dễ thôi. – AppAspect

Trả lời

18

mẫu mã:

- (NSString *)applicationDocumentsDirectory { 
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 
} 

-(void)loadimage{ 
    NSString *workSpacePath=[[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"your image-name"]; 
    UIImageView *myimage=[UIImageView alloc] initWithFrame:CGRectMake(0,0,20,20)]; 
    myimage.image=[UIImage imageWithData:[NSData dataWithContentsOfFile:workSpacePath]];  
    [self.view addSubView:myimage]; 
    [myimage release]; 
} 

TNQ

+0

Mã mẫu của bạn là dành cho hình ảnh gói. Trong khi ở đây chúng ta cần lấy hình ảnh từ thư mục tài liệu đến khung nhìn hình ảnh. – AppAspect

6

Ở đây bạn có thể mất UIImageView trong View của bạn và thêm nó vào xem và làm tương tự như như sau:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *imageFilePath; 
NSError *err; 

imageFilePath = [documentsDirectory stringByAppendingPathComponent:@"image1.png"]; 

UIImageView *objImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)]; 
objImageView.image = [UIImage imageWithContentsOfFile: imageFilePath]; 
[self.view addSubVIew:objImageView.image]; 
[objImageView release]; 

Xin vui lòng cho tôi biết nếu bạn vẫn cần bất kỳ sự giúp đỡ nào.

+0

Bạn có thể thêm đường dẫn của imageview và bạn có thể trực tiếp thêm hình ảnh vào imageview từ đường dẫn đó bằng cách sử dụng: objImageView.image = [UIImage imageWithContentsOfFile: imageFilePath]; – AppAspect

4
NSArray *directoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); 
NSString *imagePath = [directoryPath objectAtIndex:0]; 
imagePath= [imagePath stringByAppendingPathComponent:@"imagename.png"]; 

NSData *data = [NSData dataWithContentsOfFile:imagePath]; 
    UIImage *img = = [UIImage imageWithData:data]; 

bạn có thể hiển thị hình ảnh này để UIImageView

8

Bạn có thể đạt được điều đó, bởi biết tên của hình ảnh của bạn, vì vậy khi bạn đang lưu trữ hình ảnh của bạn, bạn cần phải lưu trữ một số thông tin về những hình ảnh trong cơ sở dữ liệu, (hoặc một số nơi bạn có thể sử dụng nó), sau đó lấy tên của hình ảnh từ nguồn dữ liệu, và làm việc như sau:

NSString *str = [NSString stringWithFormat:@"%@.jpg",[myGlobleArrayOfImageNames objectAtIndex:imageCounter]]; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *fullImgNm=[documentsDirectory stringByAppendingPathComponent:[NSString stringWithString:str]]; 
[mainSlideShowImageView setImage:[UIImage imageWithContentsOfFile:fullImgNm]]; 
1

bạn cần dòng mã sau đây để có được UIImage từ các tài liệu thư mục

-(UIImage*)getImage:(NSString *)strImageName 
{ 
NSString *documentDirectory=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 
NSString *imagePath=[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",strImageName]]; 
UIImage *image=[UIImage imageWithContentsOfFile:imagePath]; 
return image; 
} 

Gọi hàm trên và sử dụng đối tượng UII để đặt bất cứ nơi nào bạn muốn. Bạn cũng có thể viết phương thức này trong một lớp chung để sử dụng nó ở mọi nơi.

0

Thêm vào những câu trả lời tôi nhận thấy thực hành tốt của nó để kiểm tra xem các tập tin tồn tại như vậy ...

NSString *directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
NSString *file = [directory [email protected]"my_image.jpg"]; 
if ([[NSFileManager defaultManager] fileExistsAtPath:file]) { 
    self.myimageview = [UIImage imageWithData:[NSData dataWithContentsOfFile: file]]; 

} 
0

đơn giản Sử dụng Bộ luật này

NSFileManager *filemgr =[NSFileManager defaultManager]; 
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 
NSString *docsDir = [dirPaths objectAtIndex:0]; 
NSString *dataPath = [docsDir stringByAppendingPathComponent:@"/yourdirectory/image.jpg"]; 
NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL fileURLWithPath:dataPath]]; 
self.imageview.image = [[UIImage alloc] initWithData:imgData]; 
Các vấn đề liên quan