项目里面有须要摄像头/相册编程和图片上传的题目,在这里总结一下。



【项目组常识】



iphone中图像凡是存储在4个处所【相册、应用法度包、沙盒、Internet】,经由过程这4个源,我们就可以存取应用图片。




  • 相册


    iphone的相册包含摄像头菲林+用户策画机同步的项目组照片。用户可以经由过程UIImagePickerController类供给的交互对话框来从相册中选择图像。然则,重视:相册中的图片机械路径无法直接从应用法度接见,只能经由过程终端用户去选择和应用相册图片




  • 应用法度包


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




  • 沙盒


    借助沙盒,我们可以把图片存储到Documents、Library、tmp文件夹中。这些文件均可有应用法度读取,且可以经由过程文件路径创建图像。尽管沙盒外的项目组从技巧上说是可行的,然则apple注解这些项目组不在appstore应用法度容许接见的局限之内。




  • Internet


    应用法度可以经由过程图片的URL来接见Internet上的资料。



以上为一些小常识,来自《iphone开辟秘笈(第二版)》,可以本身去参考此书。



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




  • 从摄像头/相册获取图片


    方才在上方的常识中提到从摄像头/相册获取图片是面向终端用户的,由用户去浏览并选择图片为法度应用。在这里,我们须要过UIImagePickerController类来和用户交互。



,UINavigationControllerDelegate>。





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

}