iOS上传图片详解

iphone中图像一般存储在4个地方【相册、应用程序包、沙盒、Internet】,经过这4个源,咱们就能够存取应用图片。app

  相册框架

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

  应用程序包ui

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

  沙盒atom

  借助沙盒,咱们能够把图片存储到Documents、Library、tmp文件夹中。这些文件都可有应用程序读取,且能够经过文件路径建立图像。尽管沙盒外的部分从技术上说是可行的,可是apple代表这些部分不在appstore应用程序容许访问的范围以内。url

  Internetspa

  应用程序能够经过图片的URL来访问Internet上的资源。orm

  以上为一些小知识,来自《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;

  }

iOS  压缩图片   以下

  //压缩图片尺寸   不带裁剪框

        float  scales = image.size.height / image.size.width;

        UIImage *normalImg;

        NSData *newData;

//(一)

        /*

         若是须要改动被压大小,调整scale,而不是kk或aa

         */

        if (image.size.width > 1000 || image.size.height > 1000) {//这里的1000就是scale,全部的都要随着改变

            if (scales > 1) {

                CGSize newSize = CGSizeMake(1000 / scales, 1000);

                UIGraphicsBeginImageContext(newSize);

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

                normalImg = UIGraphicsGetImageFromCurrentImageContext();

            }else {

                CGSize newSize = CGSizeMake(1000 ,1000 * scales);

                UIGraphicsBeginImageContext(newSize);

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

                normalImg = UIGraphicsGetImageFromCurrentImageContext();

            }

        }

        else {

            normalImg=image;

        }

//(二)

        CGSize newSize = CGSizeMake(normalImg.size.width, normalImg.size.height);

        UIGraphicsBeginImageContext(newSize);

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

        UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext()

        //图片压缩系数

        float kk = 1.0f;

        //图片压缩系数变化步长(可变)

        float aa = 0.1f;

        //压缩后的大小

        int mm;

        mm=(int)UIImageJPEGRepresentation(newImage, kk).length;

        

        while (mm/1024 > 100) {

            if (kk > aa+aa/10) {

                kk -= aa;

                mm = (int)UIImageJPEGRepresentation(newImage, kk).length;

            }else{

                aa /= 10;

            }

        }

        newData = UIImageJPEGRepresentation(newImage, kk);//最后压缩结果

        NSLog(@"11111------------===%ld",(long)newData.length/1024);

        if (newData.length/1024 > 100) {

            return nil;

        }else{

            UIImage *image = [UIImage imageWithData:newData];

            return image;

        }

    }

 

  存储图像

  在上面咱们获取到了图片并对图片进行了压缩,经过以前的小知识了解到,将应用须要的一些图片存入沙盒是个不错的选择,并且应用程序能够直接经过路径去方法沙盒中的图片,在这里咱们将图片存入沙盒中的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];

  }

最近调用系统相册、相机发现是英文的系统相簿界面后标题显示“photos”,可是手机语言已经设置显示中文,纠结半天,最终在info.plist设置解决问题

info.plist里面添加Localized resources can be mixed YES

表示是否容许应用程序获取框架库内语言。 

相关文章
相关标签/搜索