0

我刚开始开发一个 ios 应用程序,就像 Contact 一样。它包括姓名、生日、组(学校、工作)、血型、图像.. 等。

在那里,其他数据可以通过键入来插入。但是,图像可以来自拍照,从 facebook 下载.. 等。

将图像输入 xcode 是不可能的。Bcoz,另一个图像可以一次又一次地出现。

我应该怎么做?请给我建议。

4

2 回答 2

1

您只需将 libSqlite3.dylib 添加到 Linked FrameWork 和 Lilbraries 并在 .h 文件中声明数据库变量

   //Database Variables
   @property (strong, nonatomic) NSString *databasePath;
   @property (nonatomic)sqlite3 *contactDB;

拖放 UIImageView 和名称...我声明为 imgView。

转到 .m 文件,您只需复制并粘贴该代码

- (void)viewDidLoad
 {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.


NSString *docsDir;
NSArray *dirPaths;

// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];

// Build the path to the database file
_databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"images.db"]];
//docsDir   NSPathStore2 *  @"/Users/gayathiridevi/Library/Application Support/iPhone Simulator/7.0.3/Applications/B5D4D2AF-C613-45F1-B414-829F38344C2A/Documents"  0x0895e160
NSFileManager *filemgr = [NSFileManager defaultManager];

if ([filemgr fileExistsAtPath: _databasePath ] == NO)
{
    const char *dbpath = [_databasePath UTF8String];

    if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK)
    {
        char *errMsg;
        const char *sql_stmt = "CREATE TABLE IF NOT EXISTS IMAGETB (ID INTEGER PRIMARY KEY AUTOINCREMENT,URL TEXT UNIQUE, IMAGE BLOB)";

        if (sqlite3_exec(_contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
        {

            NSLog( @"User table Not Created Error: %s", errMsg);
        }
        else
        {
            NSLog( @"User table Created: ");
        }
        sqlite3_close(_contactDB);
    }

    else {

        NSLog( @"DB Not Created");
    }
 }
 [self saveImage];
 [self showImage];   
 }

- (void)saveImage
{

sqlite3_stmt    *statement;
const char *dbpath = [_databasePath UTF8String];

if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK)
{

    NSString *insertSQL=@"INSERT INTO IMAGETB(image) VALUES(?)";

    if(sqlite3_prepare_v2(_contactDB, [insertSQL cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL)== SQLITE_OK)
    {

        UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://lh6.googleusercontent.com/-vJBBGUtpXxk/AAAAAAAAAAI/AAAAAAAAADQ/nfgVPX1n-Q8/photo.jpg"]]];
        NSData *imageData=UIImagePNGRepresentation(image);
        //sqlite3_bind_text(statement, 1, [@"" UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_blob(statement, 1, [imageData bytes], [imageData length], SQLITE_TRANSIENT);
        NSLog(@"Length : %lu", (unsigned long)[imageData length]);

    }


    if (sqlite3_step(statement) == SQLITE_DONE)
    {
        NSLog( @"Insert into row id %lld",(sqlite3_last_insert_rowid(_contactDB)));
    }
    else {

        NSLog( @"Error IN INSERT" );
    }
    sqlite3_finalize(statement);
    sqlite3_close(_contactDB);
  }  
 }

- (void)showImage
{

sqlite3_stmt    *statement;
const char *dbpath = [_databasePath UTF8String];
int i = 1;
if(sqlite3_open(dbpath,&_contactDB)==SQLITE_OK)

{
    NSString *insertSQL = [NSString stringWithFormat:@"Select IMAGE FROM IMAGETB WHERE ID = %d",i];
    if(sqlite3_prepare_v2(_contactDB,[insertSQL cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL) == SQLITE_OK) {
        while(sqlite3_step(statement) == SQLITE_ROW) {

            int length = sqlite3_column_bytes(statement, 0);
            NSData *imageData = [NSData dataWithBytes:sqlite3_column_blob(statement, 0) length:length];

            NSLog(@"Length : %lu", (unsigned long)[imageData length]);

            if(imageData == nil)
                NSLog(@"No image found.");
            else
                _imgView.image = [UIImage imageWithData:imageData];
            NSLog(@"image found.");
        }
    }
    sqlite3_finalize(statement);
}
sqlite3_close(_contactDB);
}

它是我的代码。您可以通过更改 -(void)showImage 函数中的 i 值来修复后退和前进按钮以获取详细信息

于 2014-09-11T08:38:15.983 回答
0

关于拍照或从你的库中抓取,请参阅UIImagePickerController 类参考iOS 的相机编程主题。要从 Facebook 抓取照片,您应该参考它的API,因为那是完全不同的鱼。

于 2012-12-31T20:08:20.240 回答