ios 工具类系列-----图片处理--imageUtil
============================================================博文原创,转载请声明出处蓝岩--移动互联网老兵============================================================
ios开发中需要用到不少的工具类,这里记录一下,可能会不完美,后续会不断的更新,也希望大家能分享自己好的工具。
Imageutil.h 可以从相册中选取图像,并且剪裁为指定大小保存到本地:
Imageutil.h
//// Imageutil.m// Yunho2//// Created by l on 12-11-5.////#import "Imageutil.h"#define IMAGE_TYPE @"portrait"@implementation Imageutil{ }@synthesize viewcontroller;@synthesize imageName;-(Imageutil*)initWithViewController:(UIViewController<ImageutilDelegate>*)viewcontroller_ imageName:(NSString*)imageName_{ if ((self=[super init]) !=nil) { self.viewcontroller=viewcontroller_; self.imageName=imageName_; return self; } return nil;}-(void)selectImage{ UIImagePickerController *imagePicker = [[[UIImagePickerController alloc] init]autorelease]; imagePicker.delegate = self; imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical; imagePicker.allowsEditing = YES; [viewcontroller presentModalViewController:imagePicker animated:YES];}+ (void)saveImage:(UIImage *)tempImage WithName:(NSString *)imagen{ NSData* imageData = UIImagePNGRepresentation(tempImage); NSString* documentsDirectory = [Imageutil getDocumentFolderPath]; // Now we get the full path to the file and then we write it out NSString* imageDirPath=[documentsDirectory stringByAppendingPathComponent:IMAGE_TYPE]; NSFileManager *manager = [NSFileManager defaultManager]; if ([manager fileExistsAtPath:imageDirPath]==NO) { [manager createDirectoryAtPath:imageDirPath withIntermediateDirectories:YES attributes:nil error:nil]; } [imageData writeToFile:[imageDirPath stringByAppendingPathComponent:imagen ] atomically:NO];}+ (NSString *)getDocumentFolderPath{ NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); return [paths objectAtIndex:0];}- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ UIImage *image= [info objectForKey:@"UIImagePickerControllerOriginalImage"]; UIImage* scaledImage=[self scalingAndCroppingToSize:CGSizeMake(64, 64) withImage:image]; [Imageutil saveImage:scaledImage WithName:imageName]; [viewcontroller dismissModalViewControllerAnimated:YES]; [viewcontroller didFinishPickingMedia];}+(UIImage*)getImageByName:(NSString*)imageName{ if (imageName==nil || [@"" isEqualToString:imageName]) { return nil; } NSString* documentsDirectory = [Imageutil getDocumentFolderPath]; NSString* imagePath=[documentsDirectory stringByAppendingPathComponent:IMAGE_TYPE]; return [UIImage imageWithContentsOfFile:[imagePath stringByAppendingPathComponent:imageName]];}//剪裁&压缩图片- (UIImage*) scalingAndCroppingToSize:(CGSize)targetSize withImage:(UIImage*)sourceImage{ UIImage *newImage = nil; CGSize imageSize = sourceImage.size; CGFloat width = imageSize.width; CGFloat height = imageSize.height; CGFloat targetWidth = targetSize.width; CGFloat targetHeight = targetSize.height; CGFloat scaleFactor = 0.0; CGFloat scaledWidth = targetWidth; CGFloat scaledHeight = targetHeight; //剪裁点的位置,以3*2长方形剪裁为2*2正方形为例,截取image中心部分,剪切点为(0,0.5) CGPoint thumbnailPoint = CGPointMake(0.0,0.0); if (CGSizeEqualToSize(imageSize, targetSize) == NO) { //compress scale CGFloat widthFactor = targetWidth / width; CGFloat heightFactor = targetHeight / height; //set scaleFactoras lesser scale,fit lesser scale if (widthFactor > heightFactor) scaleFactor = widthFactor; // scale to fit height else scaleFactor = heightFactor; // scale to fit width //reset the target size with the new scale factor scaledWidth = width * scaleFactor; scaledHeight = height * scaleFactor; // get cropping point if (widthFactor > heightFactor) { thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; } else if (widthFactor < heightFactor) { thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5; } } UIGraphicsBeginImageContext(targetSize); // this will crop CGRect thumbnailRect = CGRectZero; thumbnailRect.origin = thumbnailPoint; thumbnailRect.size.width = scaledWidth; thumbnailRect.size.height = scaledHeight; //corpping image to special rectangle [sourceImage drawInRect:thumbnailRect]; newImage = UIGraphicsGetImageFromCurrentImageContext(); if(newImage == nil) NSLog(@"could not scale image"); //pop the context to get back to the default UIGraphicsEndImageContext(); return newImage;}-(void)dealloc{ [super dealloc]; [viewcontroller release]; [imageName release];}@end