简体   繁体   English

如何通过SQLite在UIImageView上读取和显示本地图像

[英]How do I read in & display a local image on UIImageView via SQLite

How may I modify, read in an image path from SQLite DB, & display it via an UIImageView? 如何修改,从SQLite DB中读取图像路径并通过UIImageView显示它? In other words, this application will work in offline mode & i have the images at hand. 换句话说,此应用程序将在离线模式下工作,并且我手边有图像。 How will i go about this then, tgt with sqlite? 那么,如何与sqlite进行此操作? (retrieve images by storing their image path name @ sqlite) (通过存储图像路径名@ sqlite来检索图像)

Sample code I have for now: 我现在有示例代码:

- (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];
    }
}

You are missing a lot the way I see it. 你以我的方式想念很多东西。 If your images are coming from a website then you should use a webView to display it. 如果图像来自网站,则应使用webView进行显示。 If you are planning to display images in UIImageView, you should first download them onto your device from the url that points to that image and save it in the sandbox. 如果打算在UIImageView中显示图像,则应首先从指向该图像的URL将其下载到设备上,然后将其保存在沙箱中。 Then you store the path of this downloaded image in your sqlitedb. 然后,将此下载图像的路径存储在sqlitedb中。 Now when you want to load this image you just access this image using the path that you have in your sqlitedb and render it. 现在,当您要加载该图像时,只需使用sqlitedb中具有的路径访问该图像并进行渲染即可。 How do you think by just saving the imageName image013.png and not downloading the image itself will render the image for you in UIImageView. 您如何看待,只需保存imageName image013.png而不下载图像本身,即可在UIImageView中为您呈现图像。 Just think where your UIImageView will go and find this image if it's not already downloaded onto your device. 只需考虑一下UIImageView的去向,如果尚未下载到设备上就可以找到该图像。 For going and finding on the web directly, you need a UIWebView and not an UIImageView 要直接在网上查找,您需要一个UIWebView而不是一个UIImageView


If you are packaging your images as resources with your app binary, you can load them as shown below: 如果您要使用应用程序二进制文件将图像打包为资源,则可以如下所示加载它们:

UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"myimagefile" ofType:@"png"]];
[self.testView.imgView setImage:img];

where myimagefile is the name of your file and ofType is the type of image. 其中myimagefile是文件的名称,ofType是图像的类型。 .png or .jpg or whatever. .png或.jpg或其他格式。

If you have anymore doubts. 如果您还有其他疑问。 Please come back. 请回来

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM