如何将图像从文档目录显示到imageview?

时间:2013-08-09 12:16:09

标签: iphone ios sqlite

我正在使用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 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分: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]];

希望你的推荐

答案 2 :(得分: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];

答案 3 :(得分: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];