내 앱에서 UIImagePickerController
을 통해 선택한 이미지를 JPEG 이미지 만 허용하는 데이터베이스로 업로드하고 싶습니다. 나는 여기에서 많은 질문을 봤는데, 다른 포럼에서는 여전히 작동하지 않았다. 내가 볼 수없는 실수가 있다면 내 코드를 확인할 수 있기를 바랍니다. 문서는 이미지에 대한 이미지 정보, 화상 제목 및 위치 데이터와, 업로드 용 완전한 방법이다 :는 UIImage
후Multipart/Formdata 이미지 업로드,
- (void) uploadPic{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *destinationFilePath = [[NSString alloc] initWithFormat: @"%@/%@.jpeg", documentsDirectory, self.chosenImage.imgTitle];
NSURL *fileURL = [[NSURL alloc]initWithString:destinationFilePath];
NSLog(@"will upload file: %@", destinationFilePath);
NSData *imageURLdata = [[NSData alloc]initWithContentsOfURL:fileURL]; (*1)
NSData *imageData = UIImageJPEGRepresentation(self.chosenImage, 90); (*2)
//Here i tried 2 ways of getting the data for uploading, but both don't work.
// create the URL
NSURL *postURL = [NSURL URLWithString:@"http://*********/PictureUpload"];
// create the connection
NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:postURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
// change type to POST (default is GET)
[postRequest setHTTPMethod:@"POST"];
// just some random text that will never occur in the body
NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo";
// header value, user session ID added
NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",
sessionID];
// set header
[postRequest addValue:headerBoundary forHTTPHeaderField:@"Content-Type"];
// create data
NSMutableData *postBody = [NSMutableData data];
// title part
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"title\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[self.chosenImage.imgTitle dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// desc part
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"desc\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[self.chosenImage.imgDescription dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// latitude part
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"latitude\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[self.chosenImage.latitude dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// longitude part
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"longitude\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[self.chosenImage.longitude dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// media part
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@.jpeg\"\r\n", self.chosenImage.imgTitle ] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Type: image/jpeg\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[NSData dataWithData:imageURLdata]];
[postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// final boundary
[postBody appendData:[[NSString stringWithFormat:@"--%@--\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *s = [[NSString alloc] initWithData:postBody encoding:NSASCIIStringEncoding];
NSLog(@"%@", s);
// add body to post
[postRequest setHTTPBody:postBody];
// pointers to some necessary objects
NSURLResponse* response;
NSError* error;
// synchronous filling of data from HTTP POST response
NSData *responseData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:&response error:&error];
if (error)
{
NSLog(@"Error: %@", [error localizedDescription]);
}
// convert data into string
NSString *responseString = [[NSString alloc] initWithBytes:[responseData bytes]
length:[responseData length]
encoding:NSUTF8StringEncoding];
// see if we get a welcome result
NSLog(@"%@", responseString);
[self responseHandler:responseString];
}
GEOimage
chosenImage
가 CGImageRef
통해 생성은 ImagePickerController
에서 선택된다. 방법 nr. 업로드에 NSData
을 얻으려면 * 1이 좋지 않습니다. 여기서는 이미지가 문서 디렉토리에서 선택되고 여기에 이미지에 대한 모든 EXIF 정보가 삭제되기 때문입니다. 두 가지 방법으로 이미지 파일 형식이 지원되지 않는 (JPEG 예상) 데이터베이스의 응답을받습니다. 메소드 nr로 생각했습니다. * 2 JPEG 이미지를 보내고 있지만 아마도 multipart/formdata 프로세스 전체에서 실수를 저질렀을 것입니다.
파일 시스템의 원본 파일에 대한 URL을 가져 오려고했지만 매우 어려웠습니다. 이미지에서 자산 URL 만 가져 왔습니다. 사전에
덕분에
S4lfish