簡體   English   中英

ios無法顯示數據庫中的圖像

[英]ios unable to display images from database

嘿家伙我正在創建一個程序,從數據庫中取出圖像並將其存儲在數組中。 但在將它們存儲在數組后,我無法顯示它們。 引發異常,當我用斷點檢查它時,它給了我一行異常,這是我的代碼和那一行,

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

我該怎么辦,請提前幫助...

圖像在數據庫存儲中的NSData不是這樣的UIImage你把NSData的數據庫,並從數據庫中獲取的NSData從這個NSData的圖像獲取和... ... DATABSE店面形象不是很好的概念...保存圖像文件的文件夾和保存路徑數據庫中的圖像...並從此路徑獲取圖像以顯示....

如果你將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];

我認為你沒有保留slideImages 如果要填充正確的UIImage對象,請保留數組slideImages

我假設您將NSData類型存儲在slideImages中。 在這種情況下,您應該使用[UIImage imageWithData:(NSData *)]初始化圖像對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM