简体   繁体   English

将照片从iPhone应用程序上传到Rails服务器

[英]Upload Photo from iphone app to rails server

I am building an iphone app that lets users upload photos to a rails-backed web service. 我正在构建一个iPhone应用程序,该应用程序允许用户将照片上传到Rails支持的Web服务。 I am testing the app on my device and I am able to take a photo then post it- however the photoData is returned as null. 我正在设备上测试该应用程序,并且可以拍照然后将其发布-但是photoData返回为null。 This is the xcode log after creating a post: 这是创建帖子后的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;
}

What am I missing from the implementation to actually post the data to the server? 我实际上缺少将数据发布到服务器的实现?

On the rails side I am using carrierwave and miniMagick as uploaders and using :fog storage to save everything to S3. 在铁轨方面,我使用的是carrierwave和miniMagick作为上传器,并使用:fog存储将所有内容保存到S3。 (That is the goal at least- but that isn't happening.) My post model has these properties: (至少这是目标,但那没有实现。)我的帖子模型具有以下属性:

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

The save method is like this: 保存方法是这样的:

- (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];
}

And the actual method that gets called onSave in the photoViewController is this: 在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);
            }
        }];
 }

The problem is that the photoData is not saved to the server. 问题是photoData没有保存到服务器。 Any ideas why? 有什么想法吗?

Initially I had a precompiled default photo in rails: assets/images/nophoto.png - but any time I took a picture, this default would override the new photo and get posted, which is obviously not what I want. 最初,我在rails中有一个预编译的默认照片:assets / images / nophoto.png-但是每次我拍照时,该默认照片都会覆盖新照片并发布,这显然不是我想要的。 So I deleted that, and now I am getting null. 所以我删除了它,现在我得到的是空值。 Any ideas? 有任何想法吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM