我怎么读&通过SQLite在UIImageView上显示本地图像

时间:2011-05-18 06:44:15

标签: iphone objective-c cocoa-touch

我如何修改,从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];
    }
}

1 个答案:

答案 0 :(得分:0)

你看错的方式很多。如果您的图像来自网站,那么您应该使用webView来显示它。如果您计划在UIImageView中显示图像,则应首先从指向该图像的URL将其下载到您的设备上并将其保存在沙箱中。然后将此下载图像的路径存储在sqlitedb中。现在,当您要加载此图像时,只需使用sqlitedb中的路径访问此图像并进行渲染即可。您如何看待只保存imageName image013.png而不下载图像本身将在UIImageView中为您呈现图像。如果UIImageView尚未下载到您的设备上,只需考虑UIImageView将去哪里找到该图像。要直接在网上进行查找,您需要一个UIWebView而不是UIImageView


如果您使用app二进制文件将图像打包为资源,则可以按如下所示加载它们:

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

其中myimagefile是文件的名称,ofType是图像的类型。 .png或.jpg或其他什么。

如果你有任何疑问。请回来。