[英]Upload image using xcode (MySQL + PHP)
我正在嘗試使用XCODE中的PHP將圖像上傳到MySQL數據庫,但是由於它不起作用,我遇到了一些問題。 當用戶插入文本時,它工作正常,但是當涉及到圖像時,我做不到。
我已經閱讀了一些教程和示例,但是它們在我的xcode項目中不起作用。 您能幫我解決這個問題嗎?
那是我的代碼:
- (IBAction)uploadPhoto:(UIButton *)sender {
NSLog(@"Photo enviada");
/*
NSString *strURL = [NSString stringWithFormat:@"http://example.com/upload.php?image=%@",_imageView.image];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
NSLog(@"%@", strResult);
*/
NSData *imageData = UIImageJPEGRepresentation(_imageView.image, 90);
NSString *urlString = @"http://autograpp.com/upload.php";
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
}
但是我遇到了一個錯誤:
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
ARC禁止顯式發送自動釋放消息。
提前致謝。
您正在使用- autorelease
在已啟用ARC的項目中自動發布; 這既沒有必要,也不允許。 該代碼可能更舊,並且早於ARC(自動引用計數)。
如果您更換
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
與
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
它應該工作。
在ARC中,您不必再保留/釋放對象。 這是自動完成的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.