좋아, 나는 해결책이있다. 다른 사람들이 내 경험을 통해 배울 수 있도록 내가 한 일을 가장 잘 설명 할 것입니다. 여기 간다 :
당신이 사진을 소요 아이폰 애플 리케이션이 가정 : 나는 모바일 이미지를 처리하기위한 구체적 방법을 설정 레일 측면에서
//handle the image that has just been selected
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//get the image
UIImage* image = [info valueForKey:@"UIImagePickerControllerOriginalImage"];
//scale and rotate so you're not sending a sideways image -> method provided by http://blog.logichigh.com/2008/06/05/uiimage-fix/
image = [self scaleAndRotateImage:image];
//obtain the jpeg data (.1 is quicker to send, i found it better for testing)
NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(image, .1)];
//get the data into a string
NSString* imageString = [NSString stringWithFormat:@"%@", imageData];
//remove whitespace from the string
imageString = [imageString stringByReplacingOccurrencesOfString:@" " withString:@""];
//remove <and> from string
imageString = [imageString substringWithRange:NSMakeRange(1, [imageString length]-2)];
self.view.hidden = YES;
//dismissed the camera
[picker dismissModalViewControllerAnimated:YES];
//posts the image
[self performSelectorInBackground:@selector(postImage:) withObject:imageString];
}
- (void)postImage:(NSString*)imageData
{
//image string formatted in json
NSString* imageString = [NSString stringWithFormat:@"{\"image\": \"%@\", \"authenticity_token\": \"\", \"utf8\": \"✓\"}", imageData];
//encoded json string
NSData* data = [imageString dataUsingEncoding:NSUTF8StringEncoding];
//post the image
[API postImage:data];
}[/code]
Then for the post:
[code]+(NSArray*)postImage:(NSData*) data
{
//url that you're going to send the image to
NSString* url = @"www.yoururl.com/images";
//pretty self explanatory request building
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
[request setTimeoutInterval:10000];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod: @"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:data];
NSError *requestError;
NSURLResponse *urlResponse = nil;
NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&requestError];
return [API generateArrayWithData:result];
}
을, 이것은 당신이 아마존에 이미지를 게시 할 수 있도록해야한다 Carrierwave를 통한 S3 계정 :
def post
respond_to do |format|
format.json {
#create a new image so that you can call it's class method (a bit hacky, i know)
@image = Image.new
#get the json image data
pixels = params[:image]
#convert it from hex to binary
pixels = @image.hex_to_string(pixels)
#create it as a file
data = StringIO.new(pixels)
#set file types
data.class.class_eval { attr_accessor :original_filename, :content_type }
data.original_filename = "test1.jpeg"
data.content_type = "image/jpeg"
#set the image id, had some weird behavior when i didn't
@image.id = Image.count + 1
#upload the data to Amazon S3
@image.upload(data)
#save the image
if @image.save!
render :nothing => true
end
}
end
end
이 게시물은 저에게 적합하며 매우 확장 가능해야합니다. 클래스 메소드의 경우 :
#stores the file
def upload(file)
self.path.store!(file)
end
#converts the data from hex to a string -> found code here http://4thmouse.com/index.php/2008/02/18/converting-hex-to-binary-in-4-languages/
def hex_to_string(hex)
temp = hex.gsub("\s", "");
ret = []
(0...temp.size()/2).each{|index| ret[index] = [temp[index*2, 2]].pack("H2")}
file = String.new
ret.each { |x| file << x}
file
end
이 코드는 완벽하지 않으며, 장황한 경우조차도 아닙니다. 그러나, 그것은 나를 위해 일한다. 나는 누군가가 그것을 향상시킬 수 있다고 생각한다면 제안에 개방적이다. 희망이 도움이!
먼저 사진 모델
class Photo
include Mongoid::Document
include Mongoid::Timestamps
mount_uploader :image, PhotoImageUploader
field :title, :type => String
field :description, :type => String
end
API에서 두 번째 :: V1 :: PhotosController
: 여기