Skip to content Skip to sidebar Skip to footer

Saving And Viewing Image In Ios App

I just start develop an ios app just like Contact. It include Name, Birthday, Group(School, Work), BloodType, Image.. etc. In that, other Data can insert by typing. But, image can

Solution 1:

you just ADD libSqlite3.dylib to Linked FrameWork and Lilbraries and declared database varibles in .h file

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

drag and drop UIImageView and name to that... i declared as imgView.

Goto .m file you just copy and paste that code

- (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"  0x0895e160NSFileManager *filemgr = [NSFileManager defaultManager];

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

    if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK)
    {
        char *errMsg;
        constchar *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;
constchar *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", (unsignedlong)[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;
constchar *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", (unsignedlong)[imageData length]);

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

Its my code. You fix back and Forward Button to get details by changing that i value in -(void)showImage function

Solution 2:

Regarding taking photos or grabbing from your library, see UIImagePickerController Class Reference and the Camera Programming Topics for iOS. For grabbing photos from Facebook, you should refer to its API, as that's a whole different kettle of fish.

Post a Comment for "Saving And Viewing Image In Ios App"