2017-01-30 8 views
1

iOS 응용 프로그램에서 OneDrive iOS SDK를 사용하여 OneNote 페이지를 만듭니다. 이것은 다중 이미지에 대한 게시물 요청에 대해 수행 한 코드입니다.하나의 OneNote 페이지에 여러 이미지를 업로드하는 방법은 무엇입니까?

NSMutableArray *selecedFileNames = [[NSMutableArray alloc] init]; 
NSString *date = [self formatDate]; 
NSString *start = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n" 
        "<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en-us\">\r\n" 
        "<head>\r\n" 
        "<title>Created By</title>\r\n" 
        "<meta name=\"created\" content=\"%@\" />\r\n" 
        "</head>\r\n" 
        "<body>\r\n" 
        "<p>This is <b>Photo</b> <i>Created</i> note.</p>\r\n",date]; 

for (int i = 0; i<self.selectedImages.count; i++) { 

    self.imgObject = (ImageObjects *)[self.selectedImages objectAtIndex:i]; 

    FileModel *fileModel = [FileModel new]; 
    fileModel.fileData = self.imgObject.image_data; 
    fileModel.fileName = self.imgObject.imageName; 
    fileModel.fileType = FileTypeImage; 

    self.filePath = fileModel.getFileName; 

    NSString *trimmedString = [self.filePath stringByReplacingOccurrencesOfString:@" " withString:[NSString stringWithFormat:@"%d",i]]; 
    self.filePath = [trimmedString stringByReplacingOccurrencesOfString:@":" withString:@""]; 

    NSString *middle = [NSString stringWithFormat:@"<img src=\"%@\" alt=\"image\" width=\"500\">\r\n",self.filePath]; 

    [selecedFileNames addObject:self.filePath]; 
    start = [start stringByAppendingString:middle]; 

} 

start = [start stringByAppendingString:@"</body>\r\n" 
     "</html>\r\n"]; 

NSString *boundary = [self generateBoundaryString]; 

NSString *pagesEndpoint = [NSString stringWithFormat:@"sections/%@/pages", selectedSectionId]; 
NSString *fullEndpoint = [serviceRootUrl stringByAppendingString:pagesEndpoint]; 

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ; 
[request setURL:[NSURL URLWithString:fullEndpoint]]; 
[request setHTTPMethod:@"POST"]; 

// set content type 
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]; 
[request setValue:contentType forHTTPHeaderField: @"Content-Type"]; 

NSMutableData *body = [NSMutableData data]; 


[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"Presentation\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"Content-Type:application/xhtml+xml\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"%@\r\n", start] dataUsingEncoding:NSUTF8StringEncoding]]; 

for (int i=0; i< self.selectedImages.count; i++) { 

    self.imgObject = (ImageObjects *)[self.selectedImages objectAtIndex:i]; 

    FileModel *fileModel = [FileModel new]; 
    fileModel.fileData = self.imgObject.image_data; 
    fileModel.fileName = self.imgObject.imageName; 
    fileModel.fileType = FileTypeImage; 

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\" \r\n",[selecedFileNames objectAtIndex:i]] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[@"\r\n--" dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[NSData dataWithData:fileModel.fileData]]; 
    [body appendData:[@"--\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 

} 

[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n.", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 


NSString *finalBody =[[NSString alloc] initWithData:body encoding:NSASCIIStringEncoding]; 

// set request body 
[request setHTTPBody:body]; 

if (self.client) 
{ 
    // Send the HTTP request. 
    [self sendRequest:request]; 
} 

OneNote에서 이미지가있는 페이지를 성공적으로 만들 수 있지만 페이지의 이미지를 볼 수 없습니다. 이것 좀 도와주세요. 요청시 문제가 무엇인지 알 수 없습니다. 거기에 어떤 문제가 내가 만들고있어 요청입니까?

+0

이것은 Microsoft에 제공 한 샘플 코드입니다. 특정 코드를 공유 할 수 있습니까? 이미지는 어디에서 왔습니까? 멀티 파트로 요청에 이미지를 보내고 있습니까, 아니면 과 같은 HTML 이미지 URL을 참조하고 있습니까? –

답변

1

응답 코드가 "413"이면 API로 전송하는 요청의 크기를 초과하고 있음을 의미합니다. 전송하는 이미지의 총 크기에 따라 다르며 이미지 수는 다릅니다.

Base64 문자열을 통해 이미지를 보내면 크기가 33 % 증가합니다 (즉 샘플이 수행하는 것임). 멀티 파트 요청을 보내 33 % 히트를 피할 수 있습니다. 또한 JPEG와 같은 파일 형식 (PNG를 사용하는 것처럼 보이므로 이미지 크기를 줄이기 위해 추가 조치를 취해야 할 수도 있음)을 사용하여 이미지 크기를 크게 줄일 수 있습니다. 당신이 이러한 헤더가있는 경우

우리의 아이폰 OS 샘플은 정확히이 기능 createPageWithImage

NSString *attachmentPartName = @"pngattachment1"; 
NSString *date = dateInISO8601Format(); 
UIImage *logo = [UIImage imageNamed:@"Logo"]; 

NSString *simpleHtml = [NSString stringWithFormat: 
         @"<html>" 
         "<head>" 
         "<title>A simple page with an image from iOS</title>" 
         "<meta name=\"created\" content=\"%@\" />" 
         "</head>" 
         "<body>" 
         "<h1>This is a page with an image on it</h1>" 
         "<img src=\"name:%@\" alt=\"A beautiful logo\" width=\"%.0f\" height=\"%.0f\" />" 
         "</body>" 
         "</html>", date, attachmentPartName, [logo size].width, [logo size].height]; 

NSData *presentation = [simpleHtml dataUsingEncoding:NSUTF8StringEncoding]; 
NSData *image1 = UIImageJPEGRepresentation(logo, 1.0); 
NSString *endpointToRequest = [ONSCPSCreateExamples getPagesEndpointUrlWithSectionName:sectionName]; 
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:endpointToRequest parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) { 
    [formData 
    appendPartWithHeaders:@{ 
          @"Content-Disposition" : @"form-data; name=\"Presentation\"", 
          @"Content-Type" : @"text/html"} 
    body:presentation]; 
    [formData 
    appendPartWithHeaders:@{ 
          @"Content-Disposition" : [NSString stringWithFormat:@"form-data; name=\"%@\"", attachmentPartName], 
          @"Content-Type" : @"image/jpeg"} 
    body:image1]; 
}]; 

에서 수행합니다

Content-Type: multipart/form-data; boundary=0FA204EB-20EA-4E56-99EC-5EF56D600E02 

그리고이 몸을, 그것을 작동합니다.

--0FA204EB-20EA-4E56-99EC-5EF56D600E02 
Content-Disposition: form-data; name="Presentation" 
Content-Type: text/html 
<!DOCTYPE html> 
<html><head><title>Created By Photo</title> 
<meta name="created" content="2017-02-01T11:34:11+05:30" /></head><body><p>This is some <b>Photo</b> <i>Created</i> note.</p></body><h1>This is a note with image.</h1> 
<img src="name:2017-02-01060411+0000.JPG" alt="image" ></body></html> 
--0FA204EB-20EA-4E56-99EC-5EF56D600E02 
Content-Disposition: form-data; name="2017-02-01060411+0000.JPG" 
Content-Type: image/jpeg 
IMAGE 
--0FA204EB-20EA-4E56-99EC-5EF56D600E02-- 
. 
+0

제 질문을 업데이트 해주세요. 그리고 이미지는 내 문서 디렉토리에 저장되어 있으며 데이터로 변환됩니다. –

+0

알았습니다. 내 대답이 업데이트되었습니다. –

+0

어디서 AFHTTPRequestSerializer를 얻을 수 있습니까? Site One에서 OneDrive SDK를 설치했습니다. Live SDK가 OneDrive API로 대체되었습니다. –

0

이것은 하나 이상의 이미지를 기록한 것입니다.

NSMutableArray *selecedFileNames = [[NSMutableArray alloc] init]; 
NSString *date = [self formatDate]; 

NSString *pagesEndpoint = [NSString stringWithFormat:@"sections/%@/pages", selectedSectionId]; 
NSString *fullEndpoint = [serviceRootUrl stringByAppendingString:pagesEndpoint]; 

NSString *start = [NSString stringWithFormat:@"<html>" 
        "<head>" 
        "<title>Created By Photo</title>" 
        "<meta name=\"created\" content=\"%@\" />" 
        "</head>" 
        "<body>" 
        "<h1>This is a page with an image on it</h1>",date]; 

for (int i = 0; i<self.selectedImages.count; i++) { 

FileModel *fileModel = [FileModel new]; 
fileModel.fileData = self.imgObject.image_data; 
fileModel.fileName = self.imgObject.imageName; 
fileModel.fileType = FileTypeImage; 

self.filePath = fileModel.getFileName; 
UIImage *img = [UIImage imageWithData:fileModel.fileData]; 
NSString *trimmedString = [self.filePath stringByReplacingOccurrencesOfString:@" " withString:[NSString stringWithFormat:@"%d",i]]; 
self.filePath = [trimmedString stringByReplacingOccurrencesOfString:@":" withString:@""]; 

NSString *middle = [NSString stringWithFormat:@"<h1>This is image</h1>" 
        "<img src=\"name:%@\" alt=\"A beautiful image\" width=\"%.0f\" height=\"%.0f\" />\r\n",self.filePath,[img size].width, [img size].height]; 

[selecedFileNames addObject:self.filePath]; 
start = [start stringByAppendingString:middle]; 

} 

start = [start stringByAppendingString:@"</body>\r\n" 
"</html>\r\n"]; 


NSData *presentation = [start dataUsingEncoding:NSUTF8StringEncoding]; 

NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:fullEndpoint parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 


    [formData appendPartWithHeaders:@{ 
           @"Content-Disposition" : @"form-data;    name=\"Presentation\"", 
           @"Content-Type" : @"text/html"} 
     body:presentation]; 


for (int i=0; i< self.selectedImages.count; i++) { 

self.imgObject = (ImageObjects *)[self.selectedImages objectAtIndex:i]; 

FileModel *fileModel = [FileModel new]; 
fileModel.fileData = self.imgObject.image_data; 
fileModel.fileName = self.imgObject.imageName; 
fileModel.fileType = FileTypeImage; 


    [formData 
     appendPartWithHeaders:@{ 
           @"Content-Disposition" : [NSString stringWithFormat:@"form-data; name=\"%@\"", [selecedFileNames objectAtIndex:i]], 
           @"Content-Type" : @"image/jpeg"} 
     body:fileModel.fileData]; 

} 

} error:nil]; 

if (self.client) 
{ 
// Send the HTTP request. 
[self sendRequest:request]; 
}