2013-08-02 178 views
0

我正在構建一個iPhone應用程序,讓用戶可以將照片上傳到支持導軌的Web服務。 我正在測試設備上的應用程序,並且可以拍攝照片然後發佈,但是PhotoData返回爲空。 這是Xcode記錄生成一個帖子後:從iphone應用程序上傳照片到導航服務器

post =  { 
     content = Test; 
     "created_at" = "2013-08-02T19:15:05Z"; 
     id = 2; 
     photo =   { 
      thumb =    { 
       url = "<null>"; 
      }; 
      "thumb_retina" =    { 
       url = "<null>"; 
      }; 
      url = "<null>"; 
     }; 
    }; 
    success = 1; 
} 

什麼我從實現缺少實際發佈數據到服務器?

在導軌側,我使用carrierwave和miniMagick作爲上傳器,並使用:fog storage將所有內容保存到S3。 (這是最不處的目標但沒有發生。) 我的崗位模型具有以下屬性:

@property (nonatomic, strong) NSString *content; 
@property (nonatomic, strong) NSString *thumbnailUrl; 
@property (nonatomic, strong) NSString *largeUrl; 
@property (nonatomic, strong) NSData *photoData; 

的保存方法是這樣的:

- (void)saveWithProgressAtLocation:(CLLocation *)location 
         withBlock:(void (^)(CGFloat))progressBlock completion:(void (^)(BOOL, NSError *))completionBlock { 

    if (!self.content) self.content = @""; 

    NSDictionary *params = @{ 
          @"post[content]" : self.content, 

          }; 

    NSURLRequest *postRequest = [[APIClient sharedClient] multipartFormRequestWithMethod:@"POST" 
                        path:@"/posts" 
                       parameters:params 
                   constructingBodyWithBlock:^(id<AFMultipartFormData> formData) 
            { 
             [formData appendPartWithFileData:self.photoData 
                    name:@"post[photo]" 
                   fileName:@"" 
                   mimeType:@"image/png"]; 
            }]; 
    AFHTTPRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest:postRequest]; 
    [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { 
     CGFloat progress = ((CGFloat)totalBytesWritten)/totalBytesExpectedToWrite; 
     progressBlock(progress); 
    }]; 

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
     if (operation.response.statusCode == 200 || operation.response.statusCode == 201) { 
      NSLog(@"Created, %@", responseObject); 
      NSDictionary *updatedPost = [responseObject objectForKey:@"post"]; 
      [self updateFromJSON:updatedPost]; 
      [self notifyCreated]; 
      completionBlock(YES, nil); 
     } else { 
      completionBlock(NO, nil); 
     } 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     completionBlock(NO, error); 
    }]; 

    [[APIClient sharedClient] enqueueHTTPRequestOperation:operation]; 
} 

和實際方法被調用onSave在photoViewController是這樣的:

- (void)save:(id)sender { 

    Post *post = [[Post alloc] init]; 
    post.content = self.contentTextField.text; 
    post.photoData = UIImagePNGRepresentation(self.imageView.image); 

    [self.view endEditing:YES]; 

    ProgressView *progressView = [ProgressView presentInWindow:self.view.window]; 
    if (location) { 
     [post saveWithProgressAtLocation:self.locationManager.location withBlock:^(CGFloat progress) { 
      [progressView setProgress:progress]; 
     } completion:^(BOOL success, NSError *error) { 
      [progressView dismiss]; 
      if (success) { 
       [self.navigationController popViewControllerAnimated:YES]; 
      } else { 
       NSLog(@"ERROR: %@", error); 
      } 
     }]; 
} 

問題是,photoData沒有保存到服務器。任何想法爲什麼?

最初,我在rails中有一個預編譯的默認照片:assets/images/nophoto.png - 但是任何時候我拍了一張照片,這個默認設置都會覆蓋新照片並貼出來,這顯然不是我想要的。所以我刪除了,現在我變得空了。 任何想法?

回答

0

轉換圖像到數據接着編碼爲Base64字符串保存該字符串在服務器,然後從服務器檢索時,解碼的Base64字符串數據,並使用[UIImage imageWithData: data];

相關問題