ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
来源:互联网 发布:android系统源码是什么 编辑:程序博客网 时间:2024/05/16 03:35
【部分知识】
iphone中图像通常存储在4个地方【相册、应用程序包、沙盒、Internet】,通过这4个源,我们就可以存取应用图片。
相册
iphone的相册包含摄像头胶卷+用户计算机同步的部分照片。用户可以通过UIImagePickerController类提供的交互对话框来从相册中选择图像。但是,注意:相册中的图片机器路径无法直接从应用程序访问,只能通过终端用户去选择和使用相册图片
应用程序包
应用程序包可能会将图像与可执行程序、Info.plist文件和其他资源一同存储。我们可以通过本地文件路径来读取这些基于包的图像并在应用程序中显示它们。
沙盒
借助沙盒,我们可以把图片存储到Documents、Library、tmp文件夹中。这些文件均可有应用程序读取,且可以通过文件路径创建图像。尽管沙盒外的部分从技术上说是可行的,但是apple表明这些部分不在appstore应用程序允许访问的范围之内。
Internet
应用程序可以通过图片的URL来访问Internet上的资源。
以上为一些小知识,来自《iphone开发秘籍(第二版)》,可以自己去参考此书。
下面开始切入正题,从摄像头/相册获取图片,压缩图片,上传图片。
从摄像头/相册获取图片
刚刚在上面的知识中提到从摄像头/相册获取图片是面向终端用户的,由用户去浏览并选择图片为程序使用。在这里,我们需要过UIImagePickerController类来和用户交互。
使用UIImagePickerController和用户交互,我们需要实现2个协议<UIImagePickerControllerDelegate,UINavigationControllerDelegate>。
#pragma mark 从用户相册获取活动图片- (void)pickImageFromAlbum{ imagePicker = [[UIImagePickerController alloc] init]; imagePicker.delegate = self; imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;//图片来源 imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical; imagePicker.allowsEditing = YES;//是否可编辑 [self presentModalViewController:imagePicker animated:YES];}
<pre name="code" class="objc">#pragma mark 取到图片- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ UIImage *image= [info objectForKey:@"UIImagePickerControllerOriginalImage"]; if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {// UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); } theImage = [UtilMethod imageWithImageSimple:image scaledToSize:CGSizeMake(120.0, 120.0)]; UIImage *midImage = [UtilMethod imageWithImageSimple:image scaledToSize:CGSizeMake(210.0, 210.0)]; UIImage *bigImage = [UtilMethod imageWithImageSimple:image scaledToSize:CGSizeMake(440.0, 440.0)]; [theImage retain]; [self saveImage:theImage WithName:@"salesImageSmall.jpg"]; [self saveImage:midImage WithName:@"salesImageMid.jpg"]; [self saveImage:bigImage WithName:@"salesImageBig.jpg"]; [self dismissModalViewControllerAnimated:YES]; [self refreshData]; [picker release];}
在回调结束的方法中,我们对图片进行了大小的处理,为图片的上传做准备。
缩放图片
//压缩图片+ (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 [image drawInRect: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 保存图片到document- (void)saveImage:(UIImage *)tempImage WithName:(NSString *)imageName{ NSData* imageData = UIImagePNGRepresentation(tempImage); NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString* documentsDirectory = [paths objectAtIndex:0]; // Now we get the full path to the file NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName]; // and then we write it out [imageData writeToFile:fullPathToFile atomically:NO];}
上传图片
(void)upLoadSalesBigImage:(NSString *)bigImage MidImage:(NSString *)midImage SmallImage:(NSString *)smallImage{ NSURL *url = [NSURL URLWithString:UPLOAD_SERVER_URL]; ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; [request setPostValue:@"photo" forKey:@"type"]; [request setFile:bigImage forKey:@"file_pic_big"]; [request buildPostBody]; [request setDelegate:self]; [request setTimeOutSeconds:TIME_OUT_SECONDS]; [request startAsynchronous];}
- IOS学习之ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- Linux进程间通信-基于内存(共享内存)
- Spring源码学习MVC:HandlerMapping的注册与查找
- uva 10006 Carmichael Numbers
- 查询多个表放在一个集合中用union做连接
- Android与服务器端数据交互(基于SOAP协议整合android+webservice)
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- 机房收费系统5之视图
- WA 了的uva 10806 - Dijkstra, Dijkstra.
- UVA590- Always on the run
- Ehcache页面缓存的使用
- 深入浅出观察者模式
- 配置文件HttpHandlers和HttpModules接口详解
- lua gc分析(一)
- STL学习(二)set容器学习(二)-自定义比较函数