开发者

iPhone: View image from MySql database on server

开发者 https://www.devze.com 2023-01-19 17:01 出处:网络
I am newbie with iPhone programming. I have a task that upload image from iphone to server and store it in Mysql database on server. After that I retrieve the image from database and display it on iPh

I am newbie with iPhone programming. I have a task that upload image from iphone to server and store it in Mysql database on server. After that I retrieve the image from database and display it on iPhone.

  • On Upload: I used ASIFormDataRequest to send image data to server and a PHP script (uploadpicture.php) will store image data to database with blob datatype.

  • On Download: I used NSURLRequest with NSURLConnection and a PHP script (downloadpicture.php) to retrieve image from database and send it back to iphone.

This is a PHP script:

$con = mysql_connect("localhost:8889", "root", "root");
if (!$con) {
 echo "can not connect database";
 die("Could not connect: " .mysql_error());
}

echo "Connect database succesfully";

mysql_select_db("myDatabase", $con);

$result = mysql_query("select * from pictures" ,$con);

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
 echo "{$row['id']}";
 echo "{$row['title'开发者_JAVA百科]}";
 echo "{$row['description']}";
 echo "{$row['image']}";
 echo "{$row['rate']}";
 echo "{$row['numberofrate']}";
 echo "{$row['numberofdownload']}";
 echo "{$row['numberofcomment']}";
 echo "<br/>";
}


mysql_close($con);
echo "completed";

And this is the Obj-C code:

- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
 self.connection = nil;
 NSString * dataStr = [[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding];
 NSArray * lines = [dataStr componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];

 NSMutableArray * res = [NSMutableArray arrayWithCapacity:[lines count]];
 NSString * line = nil;
 Picture * pic = nil;

 for (NSUInteger i = 0; i < [lines count]; i++) {
  line = (NSString*) [lines objectAtIndex:i];
  NSArray * arr = [line componentsSeparatedByString:@","];

  pic = [[Picture alloc] init];
  pic.pictureId = [[NSDecimalNumber decimalNumberWithString:(NSString *) [arr objectAtIndex:0]] intValue];
  pic.title = (NSString*) [arr objectAtIndex:1];
  pic.description = (NSString*) [arr objectAtIndex:2];
  pic.imageData = (NSData*) [arr objectAtIndex:3];
  pic.rate = [[NSDecimalNumber decimalNumberWithString:(NSString*) [arr objectAtIndex:4]] floatValue];
  pic.numberOfRate = [[NSDecimalNumber decimalNumberWithString:(NSString*) [arr objectAtIndex:5]] intValue];
  pic.numberOfDownload = [[NSDecimalNumber decimalNumberWithString:(NSString*) [arr objectAtIndex:6]] intValue];
  pic.numberOfComment = [[NSDecimalNumber decimalNumberWithString:(NSString*) [arr objectAtIndex:7]] intValue];
  [res addObject:pic];
  [pic release];
 }


 self.picturesData = res;


 //display a image
 [self displayAImage];
 [dataStr release];
}

But the dataStr is nil in the method. Any one help me to solve this problem. Any help would be appreciated.

Thanks.


I am assuming that receivedData is an NSMutableData object. Did you implement the delegate method -connection:didReceiveData: and -connection:didReceiveResponse:?

If not, you may want to do that and in the -connection:didReceiveData: simply append the incoming data to the NSMutableData object.

If you did implement those methods already, then you might want to check to see if that delegate method is getting called and there actually is data in the temporary data storage when you finish the request. If there is nothing there, it is possible that nothing is being returned to the delegate methods.

0

精彩评论

暂无评论...
验证码 换一张
取 消