iphone - 如何将文档目录中的图像显示到 ImageView ?

标签 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。怎么做?有什么想法吗?

最佳答案

示例代码:

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

关于iphone - 如何将文档目录中的图像显示到 ImageView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18146451/

相关文章:

sql - SQLite查询性能:UPDATE与JOIN

javascript - 无法使用 JavaScript 重定向到 iPhone、iPad

iphone - 如何在社交网站上分享我的文字(如表情符号)?

iPhone NSUserDefaults 持久化难度

iPhone:如何实现信号量?

ios - 不可能的 NSInvalidArgumentException - 尝试插入 nil 对象?

ios - aws dynamodb 如何在 ios 中使用对象映射器和批处理获取

ios - 与 GPUImage 混合 : How to control the position of the second image on top of the first

java - 仅当值大于当前值时才更新字段的 SQL 语句

performance - 提高 Go 中 rows.Scan() 的性能