2011-10-18 108 views
0

嗨如何從iPhone中的指定圖像路徑檢索圖像。在我的代碼是如何從iPhone中的指定圖像路徑檢索圖像

- (void)imagePickerController:(UIImagePickerController *)UIPicker didFinishPickingImage:(UIImage *)info editingInfo:(NSDictionary *)dictionary 
{ 
    imageView.image=info; 
    [UIPicker dismissModalViewControllerAnimated:YES]; 
    NSLog(@"UImage path= %@",imageView.image); 
    NSData* imageData = UIImagePNGRepresentation(imageView.image); 
    NSString* imageName = @"MyImage.png"; 
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString* documentsDirectory = [paths objectAtIndex:0]; 
    NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName]; 
    [imageData writeToFile:fullPathToFile atomically:NO]; 
    NSLog(@"Image path = %@",fullPathToFile); 
} 

當我選擇圖像,圖像應放置,它將成功放置到圖像視圖。我得到了圖像路徑。

當我執行控制檯應用程序這表明

Image path=document = /Users/rr-mac/Library/Application Support/iPhone Simulator/4.3/Applications/46C716D0-13E9-4DF2-BC46-F1071DFCE14C/Documents/MyImage.png 

我的圖像存儲在這個指定的路徑。在這裏,我如何檢索這個圖像並將其放置到imageview。任何人都可以幫我提供代碼。

在此先感謝。

回答

1

要在目錄路徑檢索UIImage的,你應該使用:

UIImage *image = [UIImage imageWithContentsOfFile:fullPathToFile]; 

全碼:

NSString *imageName = @"MyImage.png"; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName]; 
UIImage *image = [UIImage imageWithContentsOfFile:fullPathToFile]; 
self.imageView.image = image; 
+0

感謝它工作正常。 – akk

0

如果你的圖像放在您的應用程序,你可以使用下面的代碼的文件夾下,

yourImageView.image = [UIImage imageNamed:@"MyImage.png"]; 
+0

yourImageView.image = [UIImageView的imageNamed:@「/用戶/ RR- mac/Library/Application Support/iPhone Simulator/4.3/Applications/46C716D0-13E9-4DF2-BC46-F1071DFCE14C/Documents/MyImage.png「]; 是對的這段代碼? – akk

+0

不,您不必指定完整路徑,只需按照原樣使用代碼即可。 – sElanthiraiyan

+0

它不是UIImage。它的UIImageView。 – akk

0

在之前回答您的意見,我加入這個

通過獲取doc目錄

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *docdirpath = [paths objectAtIndex:0]; 

追加圖像路徑到此docdirpath,然後在的UIImage使用它imageNamed:方法。

1

答案在你的問題本身。

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

    fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName]; 

    imageView.image = [UIImage imageNamed:fullPathToFile]; 
+0

- (void)viewDidLoad { \t NSString * imageNames = @「MyImage.png」; \t的NSArray *路徑= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, \t \t \t \t \t \t \t \t \t \t \t \t \t \t NSUserDomainMask,YES); \t NSString * documentsDirectory = [paths objectAtIndex:0]; \t \t fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageNames]; \t \t imageView。image = [UIImageView imageNamed:fullPathToFile]; \t \t } 它不工作 – akk

+1

感謝它工作正常 – akk

相關問題