[英]How do I read in & display a local image on UIImageView via SQLite
如何修改,從SQLite DB中讀取圖像路徑並通過UIImageView顯示它? 換句話說,此應用程序將在離線模式下工作,並且我手邊有圖像。 那么,如何與sqlite進行此操作? (通過存儲圖像路徑名@ sqlite來檢索圖像)
我現在有示例代碼:
- (void)viewDidLoad
{
[super viewDidLoad];
appDelegate = (SQLAppDelegate *)[[UIApplication sharedApplication] delegate];
//start with index 0
currentStepIndex = 0;
Resolution *resolutionObj = [appDelegate.resolutionArray objectAtIndex:currentStepIndex];
[resolutionDescription setText:resolutionObj.stepDescription];
//checking for null string
//replicate of ifelse @goToNextStep & @goToLastStep (needed for initial load of image)
if (resolutionObj.imageDescription != NULL)
{
//if goes in here, then image description is not null
NSURL *imageURL = [NSURL URLWithString:resolutionObj.imageDescription];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
[resolutionImage setImage:[UIImage imageWithData:imageData]];
}
else
{
//if goes in here, then image description is NULL
//show empty image
[resolutionImage setImage:NULL];
}
}
你以我的方式想念很多東西。 如果圖像來自網站,則應使用webView進行顯示。 如果打算在UIImageView中顯示圖像,則應首先從指向該圖像的URL將其下載到設備上,然后將其保存在沙箱中。 然后,將此下載圖像的路徑存儲在sqlitedb中。 現在,當您要加載該圖像時,只需使用sqlitedb中具有的路徑訪問該圖像並進行渲染即可。 您如何看待,只需保存imageName image013.png而不下載圖像本身,即可在UIImageView中為您呈現圖像。 只需考慮一下UIImageView的去向,如果尚未下載到設備上就可以找到該圖像。 要直接在網上查找,您需要一個UIWebView而不是一個UIImageView
如果您要使用應用程序二進制文件將圖像打包為資源,則可以如下所示加載它們:
UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"myimagefile" ofType:@"png"]];
[self.testView.imgView setImage:img];
其中myimagefile是文件的名稱,ofType是圖像的類型。 .png或.jpg或其他格式。
如果您還有其他疑問。 請回來
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.