[英]Upload Photo from iphone app to rails server
我正在構建一個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作為上傳器,並使用: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);
}
}];
}
問題是photoData沒有保存到服務器。 有什么想法嗎?
最初,我在rails中有一個預編譯的默認照片:assets / images / nophoto.png-但是每次我拍照時,該默認照片都會覆蓋新照片並發布,這顯然不是我想要的。 所以我刪除了它,現在我得到的是空值。 有任何想法嗎?
將圖像轉換為數據,然后編碼為Base64字符串,將該字符串保存在服務器中,然后從服務器檢索時,將Base64字符串解碼為數據,並使用[UIImage imageWithData: data];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.