[英]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.