2013-08-02 126 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];

相关问题