简体   繁体   中英

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. 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. This is the xcode log after creating a post:

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. (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:

- (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. 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. So I deleted that, and now I am getting null. Any ideas?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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