ios无法显示数据库中的图像

时间:2013-03-05 05:46:42

标签: ios arrays sqlite

嘿伙计们我正在创建一个从数据库中获取图像并将其存储在数组中的程序。但在将它们存储在数组后,我无法显示它们。引发异常,当我用断点检查它时,它给了我一行异常,这是我的代码和那一行,

 data=[MyDatabase new];
    slideImages=[data OpenMyDatabase:@"SELECT pic_name FROM exterior":@"pic_name"];
    [self putImageViewsInScrollView:slideImages.count];
       self.FullScreenImageScroller.delegate=self;



}


-(void) putImageViewsInScrollView:(int)numberOfImageViews
{

     for(int i=0 ;i< numberOfImageViews; i++)
    {

        UIImageView *fullScreenImageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:i]]];
        fullScreenImageView.frame = CGRectMake((WIDTH_OF_IMAGE * i)  , 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);
        fullScreenImageView.image= [UIImage imageNamed:[slideImages objectAtIndex:i]];
        [self.FullScreenImageScroller addSubview:fullScreenImageView];
    }

    [self.FullScreenImageScroller setContentSize:CGSizeMake(WIDTH_OF_SCROLL_PAGE * ([slideImages count]), HEIGHT_OF_IMAGE)];
    [self.FullScreenImageScroller setContentOffset:CGPointMake(0, 0)];
    [self.FullScreenImageScroller scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO];

}

这是断点给出异常的代码行,

UIImageView *fullScreenImageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:i]]];

我现在该怎么办,请提前帮助,谢谢......

3 个答案:

答案 0 :(得分:1)

图像存储在数据库中 NSData 而非 UIImage ,因此您将 NSData 放入数据库并获取 NSData 来自数据库并从此 NSData 获取图像...并且在数据库中存储图像并不是一个好的概念...将图像保存在文档文件夹中并保存图像在数据库中的路径...并从中获取图像路径......

如果您将NSData存储在数据库中,则通过

从这行代码中获取图像
UIImage *image = [UIImage imageWithData:(NSData *)];

否则,如果您想在文档文件夹中使用此图像,请使用此代码

在App中保存图片

-(void) saveImage:(UIImage *)image withFileName:(NSString *)imageName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {
    if ([[extension lowercaseString] isEqualToString:@"png"]) {
        [UIImagePNGRepresentation(image) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"png"]] options:NSAtomicWrite error:nil];
    } else if ([[extension lowercaseString] isEqualToString:@"jpg"] || [[extension lowercaseString] isEqualToString:@"jpeg"]) {
        [UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil];
    } else {
        ALog(@"Image Save Failed\nExtension: (%@) is not recognized, use (PNG/JPG)", extension);
    }
}

and save this image path in database 

首先从数据库中获取此路径,然后使用此代码

从网址获取图片

-(UIImage *) getImageFromURL:(NSString *)fileURL {
    UIImage * result;

    NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
    result = [UIImage imageWithData:data];

    return result;
}

将图像插入SQLite:

sqlite3_bind_blob(compiledStatement,i, [image_data bytes], [image_data length], SQLITE_TRANSIENT);

从SQLite获取图片:

NSData *dataForCachedImage = [[NSData alloc] initWithBytes:sqlite3_column_blob(compiledStatement, i) length: sqlite3_column_bytes(compiledStatement, i)];           
UIImage *img = [UIImage imageWithData:dataForCachedImage];

答案 1 :(得分:1)

我认为你没有保留slideImages。如果要填充正确的UIImage对象,请保留数组slideImages

答案 2 :(得分:0)

我假设您将NSData类型存储在slideImages中。 在这种情况下,您应该使用[UIImage imageWithData:(NSData *)]来初始化图像对象。