上传用户头像

- (IBAction)camera:(id)sender {

UIImagePickerController*imagePickerController = [[UIImagePickerControlleralloc]init];

imagePickerController.delegate=self;

imagePickerController.allowsEditing=YES;

imagePickerController.sourceType=UIImagePickerControllerSourceTypeCamera;

[selfpresentViewController:imagePickerControlleranimated:YEScompletion:^{}];

}

- (IBAction)pic:(id)sender {

UIImagePickerController*imagePickerController = [[UIImagePickerControlleralloc]init];

imagePickerController.delegate=self;

imagePickerController.allowsEditing=YES;

imagePickerController.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;

[selfpresentViewController:imagePickerControlleranimated:YEScompletion:^{}];

}

- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info

{

[pickerdismissViewControllerAnimated:YEScompletion:^{}];

UIImage*image = [infoobjectForKey:UIImagePickerControllerEditedImage];

/*此处info有六个值

* UIImagePickerControllerMediaType; // an NSString UTTypeImage)

* UIImagePickerControllerOriginalImage;  // a UIImage原始图片

* UIImagePickerControllerEditedImage;    // a UIImage裁剪后图片

* UIImagePickerControllerCropRect;       // an NSValue (CGRect)

* UIImagePickerControllerMediaURL;       // an NSURL

* UIImagePickerControllerReferenceURL    // an NSURL that references an asset in the AssetsLibrary framework

* UIImagePickerControllerMediaMetadata    // an NSDictionary containing metadata from a captured photo

*/

//保存图片至本地,方法见下文

UIImage*scaleImageV= [ViewControllerimageWithImageSimple:imagescaledToSize:CGSizeMake(10,10)];

[selfsaveImage:scaleImageVwithName:@"currentImage.png"];

self.picStrUrl= [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"currentImage.png"];

UIImage*savedImage = [[UIImagealloc]initWithContentsOfFile:self.picStrUrl];

_isFullScreen=NO;

[self.imageVsetImage:savedImage];

self.imageV.tag=100;

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker

{

[selfdismissViewControllerAnimated:YEScompletion:^{}];

}

#pragma mark -保存图片至沙盒

- (void) saveImage:(UIImage*)currentImage withName:(NSString*)imageName

{

NSData*imageData =UIImageJPEGRepresentation(currentImage,0.2);

//获取沙盒目录

self.picStrUrl= [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:imageName];

//将图片写入文件

[imageDatawriteToFile:self.picStrUrlatomically:NO];

}

+ (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize

{

// Create a graphics image context

UIGraphicsBeginImageContext(newSize);

// Tell the old image to draw in this new context, with the desired

// new size

[imagedrawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

// Get the new image from the context

UIImage* newImage =UIGraphicsGetImageFromCurrentImageContext();

// End the context

UIGraphicsEndImageContext();

// Return the new image.

returnnewImage;

}

- (IBAction)upload:(id)sender {

UIImage*savedImage = [[UIImagealloc]initWithContentsOfFile:self.picStrUrl];

self.imageV2.image=savedImage;

NSData* data =UIImageJPEGRepresentation(savedImage,0.1);

AFHTTPRequestOperationManager*manager = [AFHTTPRequestOperationManagermanager];

manager.responseSerializer.acceptableContentTypes= [NSSetsetWithObjects:@"application/json",@"text/html",@"text/json",@"text/javascript",nil];

manager.requestSerializer= [AFJSONRequestSerializerserializer];

manager.responseSerializer= [AFJSONResponseSerializerserializer];

[manager.requestSerializersetValue:@"application/json"forHTTPHeaderField:@"Accept"];

[manager.requestSerializersetValue:@"application/json; charset=utf-8"forHTTPHeaderField:@"Content-Type"];

NSDictionary* dic =

@{

@"key":@"token_key"};

[managerPOST:@"接收地址"

parameters:dicconstructingBodyWithBlock:^(id formData) {

[formDataappendPartWithFileData:dataname:@"pic"fileName:@"avatar.png"mimeType:@"image/png"];

}success:^(AFHTTPRequestOperation*operation,idresponseObject) {

NSLog(@"responseObject=%@",responseObject);

}failure:^(AFHTTPRequestOperation*operation,NSError*error) {

NSLog(@"失败:%@",error);

}];

}

你可能感兴趣的:(上传用户头像)