2010-10-13 153 views
1

查看圖像我是新手與iPhone編程。我有一個上傳圖片從iPhone到服務器,它在MySQL數據庫存儲在服務器的任務。之後,我從數據庫中檢索圖像並將其顯示在iPhone上。iPhone:從MySQL數據庫服務器上

  • 在上傳: 我用ASIFormDataRequest到的圖像數據發送到服務器和PHP腳本(uploadpicture.php)將圖像數據存儲到數據庫BLOB數據類型。

  • 有關下載: 我用NSURLRequest與NSURLConnection和PHP腳本(downloadpicture.php)從數​​據庫中檢索圖像並將其發送回iphone。

這是一個PHP腳本:

$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']}"; 
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"; 

這是與obj-C代碼:

- (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]; 
} 

但dataStr是在方法爲零。任何人都可以幫助我解決這個問題。任何幫助,將不勝感激。

謝謝。

回答

0

我假設receivedDataNSMutableData對象。你實現委託方法-connection:didReceiveData:-connection:didReceiveResponse:

如果不是,您可能想要這樣做,並在-connection:didReceiveData:只需將傳入數據附加到NSMutableData對象。

如果您確實已經實現了這些方法,那麼你可能要檢查,看看是否委託方法獲取調用,實際上有數據在臨時數據存儲,當你完成的請求。如果沒有任何內容,可能沒有任何內容返回給委託方法。

相關問題