2013-05-06 3 views
4

내 서버에 이미지를 업로드해야하는 프로젝트에서 작업하고 있습니다. 내 이미지의 바이너리 데이터를 데이터베이스의 BLOB 데이터 유형 필드에 저장하려고합니다. 따라서 이미지를 바이너리 형식으로 변환해야합니다. 그래서 그것은 서버 데이터베이스에 저장할 수 있습니다.iOS에서 이미지를 이진 형식으로 변환하는 방법?

그렇다면 이진 형식으로 이미지를 변환하는 방법은 무엇입니까? 제발 조언.

+5

CoreGraphics의 UIImagePNGRepresentation (UIImage * image) 메서드를 사용하면 NSData를 반환하고이 방법으로 저장합니다. – Buntylm

+1

감사합니다. @BuntyMadan 그러나 이미지를 바이너리 형식으로 변환하지 않습니다. –

+0

하지만 NSData 원시 데이터를 서버로 보내고 UIImage로 변환 할 수 있습니다. – Buntylm

답변

11

UIImagePNGRepresentation(UIImage *image)CoreGraphics '메서드를 사용하면 NSData을 반환하고 저장할 수 있습니다. 그리고 다시 변환하려는 경우 UIImage[UIimage imageWithData:(NSData *data)] 메서드를 사용하여 만듭니다. 요구 사항에 따라

NSData *data = UIImageJPEGRepresentation(image, 1.0); 
//OR 
NSData *data = UIImagePNGRepresentation(image); 

:

데모 서버에

- (void)sendImageToServer { 
     UIImage *yourImage= [UIImage imageNamed:@"image.png"]; 
     NSData *imageData = UIImagePNGRepresentation(yourImage); 
     NSString *postLength = [NSString stringWithFormat:@"%d", [imageData length]]; 

     // Init the URLRequest 
     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
     [request setHTTPMethod:@"POST"]; 
     [request setURL:[NSURL URLWithString:[NSString stringWithString:@"http://yoururl.domain"]]]; 
     [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
     [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
     [request setHTTPBody:imageData]; 

     NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
     if (connection) { 
      // response data of the request 
     } 
     [request release]; 
} 
0

사용하여있는 UIImage를 보낼 수 있습니다.