[英]How can i display images from document directory to image view?
我想顯示文檔目錄中的圖像...我得到了目錄的內容。 但是當我想顯示圖像時,它什么也不顯示...我在文檔目錄中簽到了。
有誰能夠幫助我??? 問題出在哪兒???
我的密碼
NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:newDir error:nil];
setImage.image = [UIImage imageWithContentsOfFile:[filePathsArray objectAtIndex:0]];
嘿,我得到了答案.....請點擊此鏈接
感謝每個人的寶貴幫助........
我認為您將UIImage
與UIImageView
混淆了。 創建UIImage
setImage之后,需要將其設置為UIImageView:
UIImageView *myImageView = [[UIImageView alloc] initWithImage:setImage];
那么您需要將myImageView添加到您的視圖中:
[self.view addSubview:myImageView];
編輯 :
檢查文件是否存在於您使用fileExistsAtPath
的路徑並記錄輸出:
if ([[NSFileManager defaultManager] fileExistsAtPath:myPath]) {
NSLog(@"file exists!");
}
myPath將是您要獲取的路徑的NSString。 我認為您很可能沒有正確獲取文件的路徑。 您確定圖像在文檔目錄中嗎? 如果您使用的是模擬器,則始終可以使用finder查找應用程序沙箱中的文件和文件夾。 嘗試通過以下方法手動查找圖像
~/Library/Application Support/iPhone Simulator/x.x/Applications/
希望這可以幫助。
使用此代碼。
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString *docDirectory = [sysPaths objectAtIndex:0];
NSString *imagePath=[docDirectory stringByAppendingPathComponent:@"your image-name"];
UIImageView *myimage=[UIImageView alloc] initWithFrame:CGRectMake(0,0,20,20)];
myimage.image=[UIImage imageWithData:[NSData dataWithContentsOfFile:imagePath]];
[self.view addSubView:myimage];
[myimage release];
可以幫助您...
如果您的圖像在文檔目錄中,請使用以下命令:
NSString *directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *newPath = [directory stringByAppendingPathComponent:@"myMovie.m3u8"];
UIImage *image=[UIImage imageWithContentsOfFile:newPath];
您已使用文檔目錄路徑,但未提供存儲圖像的路徑。
NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:newDir error:nil];
setImage.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/imageName.png", [filePathsArray objectAtIndex:0]]];
嘗試這個,
NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:newDir error:nil];
NSString *str_imgpath = [NSString stringWithFormat:@"%@/imageName.png", [filePathsArray objectAtIndex:0]]
imgView.image = [UIImage imageWithData:[NSData dataWithContentsOfFile:str_imgpath]];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.