iOS图片上传
2016-01-28 17:39
489 查看
- (void)createimageHeader { _buttonHeader = [UIButtonbuttonWithType:UIButtonTypeCustom]; _buttonHeader.frame =CGRectMake(100,100, 100,100); _buttonHeader.backgroundColor = [UIColorblueColor]; [_buttonHeadersetTitle:@"点击"forState:UIControlStateNormal]; [self.viewaddSubview:_buttonHeader]; [_buttonHeaderaddTarget:selfaction:@selector(click:)forControlEvents:UIControlEventTouchUpInside]; } - (void)click:(UIButton *)sender { UIActionSheet *sheet = [[UIActionSheetalloc] initWithTitle:nildelegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nilotherButtonTitles:@"相册",@"拍照",nil]; [sheetshowInView:self.view]; } #pragma mark - delegate - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == [actionSheet cancelButtonIndex]) { return; } if(buttonIndex == 0) { if ([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { UIImagePickerController *imgPicker = [[UIImagePickerControlleralloc]init]; imgPicker.delegate =self; imgPicker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary; imgPicker.modalTransitionStyle =UIModalTransitionStyleCoverVertical; imgPicker.allowsEditing =YES; [selfpresentViewController:imgPicker animated:YEScompletion:nil]; } } else if(buttonIndex ==1) { if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { UIImagePickerController *imgPicker = [[UIImagePickerControlleralloc]init]; imgPicker.delegate =self; imgPicker.sourceType =UIImagePickerControllerSourceTypeCamera; imgPicker.modalTransitionStyle =UIModalTransitionStyleCoverVertical; imgPicker.allowsEditing =YES; [selfpresentViewController:imgPicker animated:YEScompletion:nil]; } else { NSLog(@"摄像头不可用"); } } }
pragma mark - imagePicker
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [selfdismissViewControllerAnimated:YEScompletion:^{ }]; /** * UIImage取出来,赋值给button */ _imageHeader = [info objectForKey:UIImagePickerControllerEditedImage]; [_buttonHeadersetBackgroundImage:[selfimageWithImageSimple:_imageHeaderscaledToSize:CGSizeMake(90,90)] forState:UIControlStateNormal]; // 在这里可以加上传图片的方法 [selfloadData]; }
- (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. return newImage; } #pragma mark -上传 - (void)loadData { AFHTTPSessionManager *manager = [AFHTTPSessionManagermanager]; manager.responseSerializer = [AFHTTPResponseSerializerserializer]; manager.requestSerializer.timeoutInterval =15; // _imageHeader 相册选择或者拍照所获得的UIImage; NSData *imageData = UIImageJPEGRepresentation(_imageHeader,1.0); NSString *urlStr = @"url"; [managerPOST:urlStr parameters:nilconstructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) { /** *解析 *在网络开发中,上传文件时,是文件不允许被覆盖,文件重名 *要解决此问题, *可以在上传时使用当前的系统时间作为文件名(当然如果有需要还可以拼接更多需要的比如用户ID) */ NSDateFormatter *formatter = [[NSDateFormatteralloc] init]; formatter.dateFormat =@"yyyyMMddHHmmss"; NSString *str = [formatter stringFromDate:[NSDatedate]]; NSString *fileName = [NSStringstringWithFormat:@"%@.jpg", str]; // 上传图片,以文件流的格式 /** *filedata : 图片的data *name : 后台的提供的字段 *mimeType : 类型 */ [formDataappendPartWithFileData:imageData name:@"icon"fileName:fileName mimeType:@"image/jpeg"]; }success:^(NSURLSessionDataTask *_Nonnull task, id _Nullable responseObject) { NSLog(@"1"); }failure:^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) { NSLog(@"2"); }]; }
相关文章推荐
- iOS 整体框架类图
- iOS JSON NSString Convert to NSDictionary
- 苹果开发的一点小总结(1)
- iOS-NSRange NSMakeRange(location,length)介绍
- iOS证书配置及详解
- iOS 开启子线程并并回到主线程
- ios 原生sdk 识别图片中的二维码
- 一个tableView的Cell中有两个点击按钮及tableView的重用
- iOS开发之旅--_isa和_imp指针
- iOS 视图在不同View之间的切换(对于convertRect:函数的一些理解)
- 我常去逛的iOS干货文章、blog等【持续更新】
- iOS 开发技术牛人博客
- iOS---Foundation(NSDecimalNumber.h)
- iOS:CYLTabBarController的具体使用实例:实现新浪微博的主流框架
- iOS开发-frame与bounds的区别
- ios textField详解
- iOS8开始默认语言有变化
- iOS开发笔记--基于面向协议MVP模式下的软件设计
- ios改变导航栏颜色及其字体颜色
- iOS 在xib或者storyboard中添加AutoLayout后,在代码中修改AutoLayout约束条件