Das Bild wird nicht aus der Datenbank abgerufen

Ich habe einige Bilder auf meinem Server gespeichert. Ich habe JSON verwendet, um entfernte Daten vom Server abzurufen. Als ich Bilder in der lokalen Datenbank speicherte, funktionierte es. Wenn ich json url benutze, funktioniert es nicht. Ich erhalte den Fehler EXC_BAD_ACCESS.

Code:

Mysof.h-Datei:

@interface Mysof : NSObject{
    NSInteger sofaId;
    NSString *sofa;
    NSString *rating;
    UIImage *photo;
}

@property (nonatomic,retain)NSString *sofa;
@property (nonatomic, assign) NSInteger sofaId;
@property (nonatomic, retain)NSString *rating;
@property (nonatomic, retain) UIImage *photo;

@end

Mysof.m-Datei:

@implementation Mysof

@synthesize sofId;
@synthesize sofa;
@synthesize rating;
@synthesize photo;

@end

Sofalistsql.h-Datei:

@interface Sofalistsql : NSObject

{
    sqlite3 *db;
}

- (NSMutableArray *) getMysofas;

@end

.m Datei:

 @implementation Sofalistsql

    - (NSMutableArray *) getMysofas{


     NSMutableArray *sofArray = [[NSMutableArray alloc] init];

    NSFileManager *fileMgr = [NSFileManager defaultManager];
    NSError *err;

    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"Empty" ofType:@"sqlite"];
    //NSLog(@"bundlePath %@", bundlePath);


    //call update function to check any data updated,
    //if there is a version difference
    //update the data base with all the required fileds.



    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    //NSLog(@"docs dir is %@", documentsDirectory);

    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"App6.sqlite"];

    [fileMgr copyItemAtPath:bundlePath toPath:appFile error:&err];


    NSURL *URL = [NSURL URLWithString:@"http://server.net/projects/mobile/jsonstring.php"];

    NSLog(@"URL is %@", URL);


     NSError *error;
     NSString *stringFromFileAtURL = [[NSString alloc]
     initWithContentsOfURL:URL
     encoding:NSUTF8StringEncoding
     error:&error];

     //NSLog(@"response is %@", stringFromFileAtURL);

     NSString *path = [documentsDirectory stringByAppendingPathComponent:@"App6.sqlite"];


     NSArray *userData = [stringFromFileAtURL JSONValue];

     // NSArray *skarray = [[NSArray alloc]init];



    NSLog(@"userdata is %@", userData);

   // int  i = 0;
    BOOL notExist = TRUE;


     for (NSArray *skarray in userData) {

     for (NSDictionary *tuser in skarray) {


           //if already exists in data base id then overwrite the name 

         if (sqlite3_open([path UTF8String], &db) == SQLITE_OK) {


      const char *sql = [[NSString stringWithFormat:@"SELECT id FROM categories where id = '%@'",[tuser objectForKey:@"id"]] cStringUsingEncoding:NSUTF8StringEncoding];     

     //NSLog(@"check stmt is %s", sql);

     sqlite3_stmt *sqlStatement,*addStmt;

     if (sqlite3_prepare_v2(db, sql, -1, &sqlStatement, NULL) == SQLITE_OK) {

       notExist = TRUE;

     while (sqlite3_step(sqlStatement) == SQLITE_ROW) {

        notExist = FALSE;


     Mysof *Mylist = [[Mysof alloc]init];
     Mylist.sofaId = sqlite3_column_int(sqlStatement, 0);
     Mylist.sofa = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,1)];
     Mylist.rating = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 2)];
     const char *raw = sqlite3_column_blob(sqlStatement, 3);
     int rawLen = sqlite3_column_bytes(sqlStatement, 3);
     NSData *data = [NSData dataWithBytes:raw length:rawLen];
     Mylist.photo = [[UIImage alloc] initWithData:data];
     [sofArray addObject:Mylist];



     }

         if(notExist){
             //NSLog(@"cat id does not exist");

             const char *sqlInsert = [[NSString stringWithFormat:@"insert into categories (id, cat_name,order_by) values ('%@', '%@', '%@')", [tuser objectForKey:@"id"], [tuser objectForKey:@"cat_name"],[tuser objectForKey:@"order_by"]] cStringUsingEncoding:NSUTF8StringEncoding];
             //NSLog(@"stmt is %s", sqlInsert);

             if(sqlite3_prepare_v2(db, sqlInsert, -1, &addStmt, NULL) != SQLITE_OK)
                 NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(db));

             if(SQLITE_DONE != sqlite3_step(addStmt))
                 NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(db));

         }


     }

     }

     }

     }

    return sofArray;


}

   @end

In der Datei viewController.m:

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


    Sofalistsql * mysofs =[[Sofalistsql alloc] init];
    self.sofas = [mysofs getMysofas];



}

Klicken Sie auf die Schaltfläche, um Bilder vom Server anzuzeigen:

-(void)click:(id)sender{


 scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0,500,320,200)];

        scrollview.showsVerticalScrollIndicator=NO;
          scrollview.showsHorizontalScrollIndicator=NO;

      scrollview.scrollEnabled=YES;

        int Width = 0;      

     //   Width = Width + 20+(i*74);


  for (int i = 0; i<[self.sofas count]; i++ ) {
            NSLog(@"index %d",i);



          //  imgView1=[[UIButton alloc]initWithFrame:CGRectMake(20+(i*74), 500, 72, 72)];

            imgView1=[[UIButton alloc]initWithFrame:CGRectMake(20+(i*74), 0, 72, 72)];

            Width = Width + 20+(i*74);

            [imgView1 setTag:i+1];

            [imgView1 addTarget:self action:@selector(dbsofaClicked:) forControlEvents:UIControlEventTouchUpInside];

            [imgView1 setImage:((Mysof *)[self.sofas objectAtIndex:i]).photo forState:UIControlStateNormal];

            [scrollview addSubview:imgView1];

          //  [myScroll addSubview:imgView1];



        }

        [scrollview setContentSize:CGSizeMake(Width,imgView1.frame.size.height+20)];

        [self.view addSubview:scrollview];



}

jsonstring.php Datei:

<?php 
    require_once('database_connection.php');
    $i = 0;
    $j = 0;
    $k = 0;
    $l = 0;
    mysql_query('SET CHARACTER SET utf8') or die("MYSQL character set error: ".mysql_error());
    $result = array();
        $sql=mysql_query("SELECT * FROM categories ORDER BY id ASC") or die(mysql_error());
        if(mysql_num_rows($sql) > 0) {
            while($res=mysql_fetch_array($sql, MYSQL_ASSOC)){
                $result[0][$i] = $res;

                $art_sql=mysql_query("SELECT * FROM product WHERE cat_id=" .$res['id']. " ORDER BY id ASC") or die(mysql_error());
                if (mysql_num_rows($art_sql) > 0){
                    while($art_res=mysql_fetch_array($art_sql, MYSQL_ASSOC)){
                        //$art_res['art_details'] = (utf8_encode(htmlentities($art_res['art_details'])));
                        //$art_res['art_details'] = htmlentities($art_res['art_details']);
                        //echo $art_res['art_details'];
                        $result[1][$k] = $art_res;
                        //print_r($art_res);    
                        $k = $k+1;  
                    }   
                }
                $i= $i+1;
            }
            $version_sql = mysql_query("SELECT * FROM version_app order by product_id desc limit 1") or die(mysql_error());
            $row = mysql_fetch_array($version_sql);
            $last_version = $row['product_id'];
            $result['2'][$l] = array('product_id' => $last_version);
            $l = $l+1;
        }


        /*echo "<pre>";
            print_r($result);
        echo "</pre>";exit;*/

    $str_enc = json_encode($result);
    //print_r($str_enc); exit;
    $str=str_replace('\r','',$str_enc);
    $str=str_replace('\t','',$str);
    $str=str_replace('\n','',$str);
    $str = stripslashes($str);
    //$str_renc = json_encode(json_decode($str));

    echo $str;

mysql_close();
?>

NSLog:

userdata is (
        (
                {
            "cat_name" = Table1;
            id = 1;
            "order_by" = 1;
        },
                {
            "cat_name" = Table2;
            id = 2;
            "order_by" = 2;
        },
                {
            "cat_name" = test;
            id = 3;
            "order_by" = 3;
        }
    ),
        (
                {
            "cat_id" = 1;
            id = 2;
            "order_by" = 1;
            "product_image" = "img.png";
        },
                {
            "cat_id" = 1;
            id = 3;
            "order_by" = 2;
            "product_image" = "img1.png";
        },
                {
            "cat_id" = 1;
            id = 4;
            "order_by" = 3;
            "product_image" = "img2.png";
        },
                {
            "cat_id" = 1;
            id = 5;
            "order_by" = 4;
            "product_image" = "img3.png";
        },
                {
            "cat_id" = 1;
            id = 6;
            "order_by" = 5;
            "product_image" = "img4.png";
        },
                {
            "cat_id" = 1;
            id = 7;
            "order_by" = 6;
            "product_image" = "img5.png";
        },

    )
)


array (
)
2013-08-16 13:19:53.044 App[3395:c07] scroll is <UIScrollView: 0x9de7cb0; frame = (0 300; 320 200); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x9de60e0>; layer = <CALayer: 0x9de4bc0>; contentOffset: {0, 0}>

Ich habe denselben Tabellennamen in der lokalen Datenbank gespeichert. Und verwendete Datenbank überschreiben. In der lokalen Datenbank habe ich Bilder in gespeichertBLOB Art. Aber in meinem Array habe ich nichts angezeigt. App funktioniert. Bilder werden jedoch nicht aus der Datenbank angezeigt.

Antworten auf die Frage(1)

Ihre Antwort auf die Frage