简体   繁体   中英

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? 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? (retrieve images by storing their image path name @ 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. 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. Then you store the path of this downloaded image in your 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. 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. Just think where your UIImageView will go and find this image if it's not already downloaded onto your device. For going and finding on the web directly, you need a UIWebView and not an 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. .png or .jpg or whatever.

If you have anymore doubts. Please come back.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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