【问题标题】:Upload Photo from iphone app to rails server将照片从 iphone 应用程序上传到 rails 服务器
【发布时间】:2013-08-02 19:34:18
【问题描述】:

我正在构建一个 iphone 应用程序,让用户可以将照片上传到支持 rails 的 Web 服务。 我正在我的设备上测试该应用程序,我可以拍照然后发布它 - 但是 photoData 返回为空。 这是创建帖子后的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;
}

在将数据实际发布到服务器时,我在实现中遗漏了什么?

在rails 方面,我使用carrierwave 和miniMagick 作为上传器,并使用:fog 存储将所有内容保存到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);
            }
        }];
 }

问题是照片数据没有保存到服务器。任何想法为什么?

最初我在 rails 中有一张预编译的默认照片:assets/images/nophoto.png - 但是任何时候我拍照时,这个默认设置都会覆盖新照片并被发布,这显然不是我想要的。所以我删除了它,现在我变得空了。 有什么想法吗?

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-23
      • 2012-05-02
      • 2010-09-12
      • 1970-01-01
      • 1970-01-01
      • 2012-02-17
      相关资源
      最近更新 更多