1

I searched a lot in google but not able to get the answer required. I am choosing photos from the image gallery and I want to store it in my application. If I have to store it in database(As BLOB), then I have to enter the name of the file(which I am not getting as I have chosen from the gallery). Can I store it anywhere else? please help. I am stuck in this from a long time. I need to extract the image and show it in map calloutview.

I am using the below code to choose photo from gallery and insert into the database

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)dictionary {
        [picker dismissModalViewControllerAnimated:YES];
    tempImg.image = image;
    NSData *imgData = UIImagePNGRepresentation(tempImg.image);
        NSLog(@"the img data %@",imgData);

sql = [NSString stringWithFormat:@"update latlng set image = '%@' where placeName = '%@'",imgData,pName.text];

The below code excuting the sql statement mentioned above

if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
        const char *sqlStatement;
        sqlStatement = [sql UTF8String];;

        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) 
        {
            if (sqlite3_step(compiledStatement))
            {
                NSLog(@"YES");
            }
        }
        else 
        {
            printf( "could not prepare statemnt: %s\n", sqlite3_errmsg(database));
        }
        sqlite3_finalize(compiledStatement);

    }sqlite3_close(database);

The below code to extract from database

NSData *data = [[NSData alloc] initWithBytes:sqlite3_column_blob(compiledStatement, 5) length:sqlite3_column_bytes(compiledStatement, 5)];

the below code to display the image in callout

UIImageView *mapImg = [[UIImageView alloc] initWithImage [UIImageimageWithData:imgData]];
pinView.leftCalloutAccessoryView = mapImg;
Droidme
  • 1,223
  • 6
  • 25
  • 45

3 Answers3

2

Convert the image obtained for photo gallery to NSData and then save it in database as a Blob,So you can easily retrieve the nsdata from database when ever you needed.

NSData *image1Data = UIImagePNGRepresentation(image1.image);
sqlite3_bind_blob(init_statement, 1, [image1Data bytes], [image1Data length], NULL);

image1 is the ImageView where i am displaying the image from the photo gallery.

All the best.

Warrior
  • 39,156
  • 44
  • 139
  • 214
  • Thanks for the answer. I tried the above now. But while converting to nsdata, the application crashes. I put an nslog over there and tried. IT jus shows the image getting encrypted but it does not stop. then crashes. plz help. – Droidme Mar 10 '11 at 05:25
  • The above code works for few images prorperly. But for few it dosent. Will it be due to the size?. I have edited my question with the code – Droidme Mar 10 '11 at 05:34
  • I dont get any crash log. It just crahses with no info. All I can see at the end of my log is numbers like 27a731a6 83cc8c89 60267940 8 – Droidme Mar 10 '11 at 05:36
  • In my case it working fine, i have checked your code too. it works fine.Are you sure that you are not leaking memory. – Warrior Mar 10 '11 at 05:48
  • @warrior No I am not. I checked that. Its working fine for me too, but crashes when I choose few images downloaded from internet. Can it be due to the size of the image? – Droidme Mar 10 '11 at 05:58
  • the second statement which u have shown, is it for retrieve them from database? I am using this statment. NSData *data = [[NSData alloc] initWithBytes:sqlite3_column_blob(compiledStatement, 5) length:sqlite3_column_bytes(compiledStatement, 5)]; – Droidme Mar 10 '11 at 06:04
  • I have given for inserting into database.Your code for retrieving is right. – Warrior Mar 10 '11 at 06:10
  • Yes its for retrieving it. Can you tel me if there s a way to get the name of the image chosen from the gallery – Droidme Mar 10 '11 at 06:21
  • @warrior this is how my image looks like in the database when I insert it(without converting to nsdata) is it right. IF so can you tel me how to extract it. I am a new bee n I am stuck with this plz help. – Droidme Mar 10 '11 at 06:25
  • @warrior. What you told works for me. but it takes time to load. I am extracting the image and putting it in the annotation callout view. so that is taking time. Can you tel me a way to solve this. I am converting it into nsdata and storing it in the dtabase – Droidme Mar 10 '11 at 06:39
  • It will not much time to covert NSData to UIImage.Please post your code.You might went wrong some were else. – Warrior Mar 10 '11 at 07:04
  • ok. Which part of my code do u want me to post? if u can give me ur mail id , I can send u my code. – Droidme Mar 10 '11 at 07:07
  • Sent the mail. please check and reply – Droidme Mar 10 '11 at 07:38
0

You can use this to retrieve the path to a directory writable by your application:

NSString *directory = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

There are various ways to access that directory, including NSData's writeToFile: methods, the NSFileManager class, the NSFileHandle class, and even C stdio functions.

Anomie
  • 92,546
  • 13
  • 126
  • 145
  • Thanks anomie. can you tel me how to store the image in the directory which I have got from the image gallery. – Droidme Mar 10 '11 at 05:27
  • You could convert it to an NSData using `UIImageJPEGRepresentation` and write it out using NSData's `writeToFile`, or you could use [`CGImageDestination`](http://developer.apple.com/library/ios/documentation/GraphicsImaging/Conceptual/ImageIOGuide/ikpg_dest/ikpg_dest.html#//apple_ref/doc/uid/TP40005462-CH219-SW3). Or there are probably other ways too. – Anomie Mar 10 '11 at 12:02
0

In my case i searching lots of google and then finaly got solution that below code its work's for me i hope its work for you

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{


NSLog(@"info: %@",info);

NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirec = [paths objectAtIndex:0];
NSString *imgePath = [documentsDirec stringByAppendingPathComponent:@"note.sqlite"];

if(sqlite3_open([imgePath UTF8String], &database) == SQLITE_OK){

    const char *sql = "insert into images (images) values (?)";
    if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) == SQLITE_OK){
        UIImage *edtedImae = [info objectForKey:UIImagePickerControllerOriginalImage];
        NSData *dataForImage = UIImagePNGRepresentation(edtedImae);
        sqlite3_bind_blob(addStmt, 1, [dataForImage bytes], [dataForImage length], SQLITE_TRANSIENT);
    }
    if(sqlite3_step(addStmt) != SQLITE_DONE){

        NSLog(@"error %s",sqlite3_errmsg(database));
    }
    else {
        NSLog(@"Insert row Id = %d",sqlite3_last_insert_rowid(database));
    }
    sqlite3_finalize(addStmt);

    }
   sqlite3_close(database);               
   [picker dismissModalViewControllerAnimated:YES];
}

thank you

nlg
  • 161
  • 1
  • 8