修改单张图片上传到服务器
#pragma mark - changeUserPhoto
- (void)changeUserPhoto {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消"
destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从手机相册选择", nil];
actionSheet.tag = 120;
[actionSheet showInView:self.view];
}
#pragma mark - actionSheet
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (actionSheet.tag == 120) {
if (buttonIndex == 0) //拍照
{
[self takePhoto];
}
else if (buttonIndex == 1) //打开本地相册
{
[self openLocalPhoto];
}
}
}
#pragma mark - TakePhoto
/**
* 开始拍照
*/
- (void)takePhoto
{
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = sourceType;
[self presentViewController:picker animated:YES completion:nil];
} else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"抱歉,你的设备不支持该功能!"
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
return;
}
}
#pragma mark - openLibrary
// 打开本地相册
- (void)openLocalPhoto
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
picker.allowsEditing = YES;
[self presentViewController:picker animated:YES completion:nil];
}
- (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info { NSString *strType = [info objectForKey:UIImagePickerControllerMediaType]; if ([strType isEqualToString:@"public.image"]) //当选择的类型是图片 { UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"]; //先把图片转成NSData NSLog(@"image = %@", image); [reader dismissViewControllerAnimated:YES completion:nil]; //关闭相册界面 //压缩图片 UIImage *scalImage = [self scaleFromImage:image]; imageView.image = scalImage; //保存到本地 NSString *strfileName = [self saveImage:scalImage]; //上传头像 NSString *strUrl = [NSString stringWithFormat:@"%@", @"http://192.168.1.108:8080/resources/user/"]; strUrl = [strUrl stringByAppendingString:@"headimg/"]; strUrl = [strUrl stringByAppendingString:@"13061622013"]; NSURL *url = [NSURL URLWithString:strUrl]; ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; [request setDelegate:self]; [request addRequestHeader:@"Content-Type" value:@"application/json; encoding=utf-8"]; [request addRequestHeader:@"Accept" value:@"application/json"]; [request setFile:strfileName forKey:@"headImg"]; [request setRequestMethod:@"POST"]; [request setDidFailSelector:@selector(ASIFormDataRequestFailed:)]; [request setDidFinishSelector:@selector(ASIFormDataRequestSuceed:)]; [request startAsynchronous]; } } #pragma mark - savaImage// 保存图像 - (NSString *)saveImage:(UIImage*)image { NSData *data; if (UIImagePNGRepresentation(image) == nil) { data = UIImageJPEGRepresentation(image, 1.0); } else { data = UIImagePNGRepresentation(image); } NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject]; NSString *filePath = [docPath stringByAppendingPathComponent:@"tianImage.png"]; BOOL result = [data writeToFile:filePath atomically:YES]; if (result) { NSLog(@"写入成功"); } return filePath; }#pragma mark - scalePhoto// 图像压缩 - (UIImage *)scaleFromImage:(UIImage *)image { if (!image) { return nil; } NSData *data = UIImagePNGRepresentation(image); CGFloat dataSize = data.length/1024; CGFloat width = image.size.width; CGFloat height = image.size.height; CGSize size; if (dataSize<=50) //小于50k { return image; } else if (dataSize<=100) //小于100k { size = CGSizeMake(width/2.f, height/2.f); } else if (dataSize<=200) //小于200k { size = CGSizeMake(width/4.f, height/4.f); } else if (dataSize<=500) //小于500k { size = CGSizeMake(width/4.f, height/4.f); } else if (dataSize<=1000) //小于1M { size = CGSizeMake(width/6.f, height/6.f); } else if (dataSize<=2000) //小于2M { size = CGSizeMake(width/10.f, height/10.f); } else //大于2M { size = CGSizeMake(width/12.f, height/12.f); } UIGraphicsBeginImageContext(size); [image drawInRect:CGRectMake(0, 0, size.width, size.height)]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); if (!newImage) { return image; } return newImage; } #pragma mark - ASIHTTPRequestDelegate - (void)ASIFormDataRequestSuceed:(ASIFormDataRequest *)request { } - (void)ASIFormDataRequestFailed:(ASIFormDataRequest *)request { NSError *error = [request error]; NSLog(@"error:%@",error);
- (void)changeUserPhoto {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消"
destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从手机相册选择", nil];
actionSheet.tag = 120;
[actionSheet showInView:self.view];
}
#pragma mark - actionSheet
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (actionSheet.tag == 120) {
if (buttonIndex == 0) //拍照
{
[self takePhoto];
}
else if (buttonIndex == 1) //打开本地相册
{
[self openLocalPhoto];
}
}
}
#pragma mark - TakePhoto
/**
* 开始拍照
*/
- (void)takePhoto
{
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = sourceType;
[self presentViewController:picker animated:YES completion:nil];
} else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"抱歉,你的设备不支持该功能!"
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
return;
}
}
#pragma mark - openLibrary
// 打开本地相册
- (void)openLocalPhoto
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
picker.allowsEditing = YES;
[self presentViewController:picker animated:YES completion:nil];
}
- (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info { NSString *strType = [info objectForKey:UIImagePickerControllerMediaType]; if ([strType isEqualToString:@"public.image"]) //当选择的类型是图片 { UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"]; //先把图片转成NSData NSLog(@"image = %@", image); [reader dismissViewControllerAnimated:YES completion:nil]; //关闭相册界面 //压缩图片 UIImage *scalImage = [self scaleFromImage:image]; imageView.image = scalImage; //保存到本地 NSString *strfileName = [self saveImage:scalImage]; //上传头像 NSString *strUrl = [NSString stringWithFormat:@"%@", @"http://192.168.1.108:8080/resources/user/"]; strUrl = [strUrl stringByAppendingString:@"headimg/"]; strUrl = [strUrl stringByAppendingString:@"13061622013"]; NSURL *url = [NSURL URLWithString:strUrl]; ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; [request setDelegate:self]; [request addRequestHeader:@"Content-Type" value:@"application/json; encoding=utf-8"]; [request addRequestHeader:@"Accept" value:@"application/json"]; [request setFile:strfileName forKey:@"headImg"]; [request setRequestMethod:@"POST"]; [request setDidFailSelector:@selector(ASIFormDataRequestFailed:)]; [request setDidFinishSelector:@selector(ASIFormDataRequestSuceed:)]; [request startAsynchronous]; } } #pragma mark - savaImage// 保存图像 - (NSString *)saveImage:(UIImage*)image { NSData *data; if (UIImagePNGRepresentation(image) == nil) { data = UIImageJPEGRepresentation(image, 1.0); } else { data = UIImagePNGRepresentation(image); } NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject]; NSString *filePath = [docPath stringByAppendingPathComponent:@"tianImage.png"]; BOOL result = [data writeToFile:filePath atomically:YES]; if (result) { NSLog(@"写入成功"); } return filePath; }#pragma mark - scalePhoto// 图像压缩 - (UIImage *)scaleFromImage:(UIImage *)image { if (!image) { return nil; } NSData *data = UIImagePNGRepresentation(image); CGFloat dataSize = data.length/1024; CGFloat width = image.size.width; CGFloat height = image.size.height; CGSize size; if (dataSize<=50) //小于50k { return image; } else if (dataSize<=100) //小于100k { size = CGSizeMake(width/2.f, height/2.f); } else if (dataSize<=200) //小于200k { size = CGSizeMake(width/4.f, height/4.f); } else if (dataSize<=500) //小于500k { size = CGSizeMake(width/4.f, height/4.f); } else if (dataSize<=1000) //小于1M { size = CGSizeMake(width/6.f, height/6.f); } else if (dataSize<=2000) //小于2M { size = CGSizeMake(width/10.f, height/10.f); } else //大于2M { size = CGSizeMake(width/12.f, height/12.f); } UIGraphicsBeginImageContext(size); [image drawInRect:CGRectMake(0, 0, size.width, size.height)]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); if (!newImage) { return image; } return newImage; } #pragma mark - ASIHTTPRequestDelegate - (void)ASIFormDataRequestSuceed:(ASIFormDataRequest *)request { } - (void)ASIFormDataRequestFailed:(ASIFormDataRequest *)request { NSError *error = [request error]; NSLog(@"error:%@",error);
}
posted on 2015-09-30 17:34 taiyang2014 阅读(259) 评论(0) 编辑 收藏 举报