图片

来源:互联网 发布:ga遗传算法c 实现 编辑:程序博客网 时间:2024/04/28 08:42

【部分知识】

  iphone中图像通常存储在4个地方【相册、应用程序包、沙盒、Internet】,通过这4个源,我们就可以存取应用图片。

  相册

  iphone的相册包含摄像头胶卷+用户计算机同步的部分照片。用户可以通过UIImagePickerController类提供的交互对话框来从相册中选择图像。但是,注意:相册中的图片机器路径无法直接从应用程序访问,只能通过终端用户去选择和使用相册图片

  应用程序包

  应用程序包可能会将图像与可执行程序、Info.plist文件和其他资源一同存储。我们可以通过本地文件路径来读取这些基于包的图像并在应用程序中显示它们。

  沙盒

  借助沙盒,我们可以把图片存储到Documents、Library、tmp文件夹中。这些文件均可有应用程序读取,且可以通过文件路径创建图像。尽管沙盒外的部分从技术上说是可行的,但是apple表明这些部分不在appstore应用程序允许访问的范围之内。

  Internet

  应用程序可以通过图片的URL来访问Internet上的资源。

  以上为一些小知识,来自《iphone开发秘籍(第二版)》,可以自己去参考此书。

  下面开始切入正题,从摄像头/相册获取图片,压缩图片,上传图片。

  从摄像头/相册获取图片

  刚刚在上面的知识中提到从摄像头/相册获取图片是面向终端用户的,由用户去浏览并选择图片为程序使用。在这里,我们需要过UIImagePickerController类来和用户交互。

  使用UIImagePickerController和用户交互,我们需要实现2个协议。

  View Code

  代码如下

  #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];

  }

  我们来看看上面的从相册获取图片,我们首先要实例化UIImagePickerController对象,然后设置imagePicker对象为当前对象,设置imagePicker的图片来源为UIImagePickerControllerSourceTypePhotoLibrary,表明当前图片的来源为相册,除此之外还可以设置用户对图片是否可编辑。

  View Code

  代码如下

  #pragma mark 从摄像头获取活动图片

  - (void)pickImageFromCamera

  {

  imagePicker = [[UIImagePickerController alloc] init];

  imagePicker.delegate = self;

  imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

  imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

  imagePicker.allowsEditing = YES;

  [self presentModalViewController:imagePicker animated:YES];

  }

  以上是从摄像头获取图片,和从相册获取图片只是图片来源的设置不一样,摄像头图片的来源为UIImagePickerControllerSourceTypeCamera。

  在和用户交互之后,用户选择好图片后,会回调选择结束的方法。

  View Code

  代码如下

  - (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];

  }

  在回调结束的方法中,我们对图片进行了大小的处理,为图片的上传做准备。

  缩放图片

  缩放图片比较简单,就直接放上代码,让大家参考一下。

  View Code

  代码如下

  //压缩图片

  + (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;

  }

  存储图像

  在上面我们获取到了图片并对图片进行了压缩,通过之前的小知识了解到,将应用需要的一些图片存入沙盒是个不错的选择,而且应用程序可以直接通过路径去方法沙盒中的图片,在这里我们将图片存入沙盒中的Documents目录下。

  View Code

  代码如下

  #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];

  }

  从Documents目录下获取图片

  要从Documents下面获取图片,我们首先需要获取Documents目录的路径。

  View Code

  代码如下

  #pragma mark 从文档目录下获取Documents路径

  - (NSString *)documentFolderPath

  {

  return [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

  }

  然后,我们便可以通过文件名,去访问获取资源了。

  View Code

  上传图片

  项目中我们使用了ASIFormHttpRequest的开源框架,http请求的部分代码如下,http返回以及相关回调方法略去。

  View Code

  代码如下

  - (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];

  }

0 0
原创粉丝点击