将照片从iphone app上传到rails服务器

时间:2013-08-02 19:34:18

标签: iphone ruby-on-rails heroku afnetworking carrierwave

我正在构建一个iphone应用程序,允许用户将照片上传到支持rails的Web服务。 我正在我的设备上测试应用程序,我可以拍照然后发布 - 但是photoData返回为null。 这是创建帖子后的xcode日志:

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作为上传器并使用:雾存储将所有内容保存到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];
}

在photoViewController中调用onSave的实际方法是:

- (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中有一个预编译的默认照片 - 但是每当我拍照时,这个默认会覆盖新照片并发布,这显然不是我想要的。所以我删除了,现在我得到了null。 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

将图像转换为数据然后编码为Base64字符串保存服务器中的字符串,然后从服务器检索时,将Base64字符串解码为数据并使用[UIImage imageWithData: data];