0

我正在使用 sqlite db 来保存文档目录的图像路径。这是我的代码:

NSData * imageData = UIImageJPEGRepresentation(image, 0.8);
    NSLog(@"Image data length== %d",imageData.length);

if (imageData != nil)
    {
        UIImage *resizedImg = [self scaleImage:[UIImage imageWithData:imageData] toSize:CGSizeMake(150.0f,150.0f)];

        NSData * imageData = UIImageJPEGRepresentation(resizedImg, 0.2);
        NSLog(@"*** Image data length after compression== %d",imageData.length);

        NSString *nameofimg=[NSString stringWithFormat:@"%@",resizedImg];

        NSString *substring=[nameofimg substringFromIndex:12];
        NSString *new=[substring substringToIndex:7];// Get image name

        NSArray *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

        NSString *documentdirectory=[path objectAtIndex:0];

        NSString *newFilePath = [NSString stringWithFormat:[documentdirectory stringByAppendingPathComponent: @"/%@.png"],new];

        [imageData writeToFile:newFilePath atomically:YES];

        imgpath=[[NSString alloc]initWithString:newFilePath];
        NSLog(@"doc img in img method === %@",imgpath);
    }


     databasepath=[app getDBPath]; // i have this method in delegate
        if (sqlite3_open([databasepath UTF8String], &dbAssessor) == SQLITE_OK)
        {
            NSString *selectSql = [NSString stringWithFormat:@"insert into imagetb(imagename,image) VALUES(\"%@\",\"%@\") ;",yourImgName,imgpath];

            NSLog(@"Query : %@",selectSql);

            const char *sqlStatement = [selectSql UTF8String];
            sqlite3_stmt *query_stmt;
            sqlite3_prepare(dbAssessor, sqlStatement, -1, &query_stmt, NULL);

            if(sqlite3_step(query_stmt)== SQLITE_DONE)
            {
                NSLog(@"home image updated. :)");
                app.houseImage=imgpath;
            }
            else
            {
                UIAlertView *alert = [[UIAlertView alloc]
                                      initWithTitle:@"Sorry" message:@"Failed To Save Home Image." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert show];
                [alert release];
            }
            sqlite3_finalize(query_stmt);
        }
        sqlite3_close(dbAssessor);

这些代码将图像保存在 sqlite 中的文档目录和路径中。现在我想将文档目录中的图像显示到 imageview。怎么做?任何想法?

4

4 回答 4

2
NSString *str = [NSString stringWithFormat:@"%@.jpg",[YourImageArray objectAtIndex:imageCounter]];
    or
     NSString *str=[[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"YourImageName"];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fullImgNm=[documentsDirectory stringByAppendingPathComponent:[NSString stringWithString:str]];
    [ImageView setImage:[UIImage imageWithContentsOfFile:fullImgNm]];

希望对您有所帮助

于 2013-08-09T12:25:00.700 回答
2

示例代码:

- (NSString *)applicationDocumentsDirectory {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

-(void)loadimage{
    NSString *workSpacePath=[[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"your image-name"];
    UIImageView *myimage=[UIImageView alloc] initWithFrame:CGRectMake(0,0,20,20)];
    myimage.image=[UIImage imageWithData:[NSData dataWithContentsOfFile:workSpacePath]];    
    [self.view addSubView:myimage];
    [myimage release];
}
于 2013-08-09T12:18:43.183 回答
0
        NSString *docDirPath =  [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] objectAtIndex:0];
        NSString *filePath = [docDirPath stringByAppendingFormat:@"myFile.png"];
        UIImage *image = [UIImage imageWithContentsOfFile:filePath];
        [imageView setImage:image];
于 2013-08-09T12:24:34.573 回答
0

如果整个路径都存储在 sqlite 中,那么您只需要使用它

NSString *imapeFilePath = PATH_FROM_DATABASE; 
self.imageView.image = [UIImage imageWithContentsOfFile:imapeFilePath];

如果文件名存储在数据库中,则使用以下

NSString *imapeFilePath =[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:YOUR_IMAGE_NAME];
self.imageView.image = [UIImage imageWithContentsOfFile:imapeFilePath];
于 2013-08-09T12:33:18.140 回答