0

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

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

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

4

3 回答 3

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 

首先是来自数据库的这条路径,然后使用这段代码

从 URL 获取图像

-(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];
于 2013-03-05T05:49:51.703 回答
1

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

于 2013-03-05T05:50:56.653 回答
0

I assume you store NSData type in slideImages. In this case, you should use [UIImage imageWithData:(NSData *)] to initialize image object.

于 2013-03-05T05:53:01.023 回答