========================================================== =============================== Original blog, reprinted please declare the source of Electronic coffee (original id blue rock) ========================================================== ================================
Many tool classes are used in iOS development. It may not be perfect to record here and will be updated in the future. I hope you can share your own tools.
Imageutil. h can select an image from the album and save it as the specified size:
Imageutil. h
//// Imageutil.h//#import <Foundation/Foundation.h>#import "NSData+Base64.h"@protocol ImageutilDelegate <NSObject>@required-(void)didFinishPickingMedia;@end@interface Imageutil : NSObject<UINavigationControllerDelegate, UIImagePickerControllerDelegate>@property (retain,nonatomic) UIViewController<ImageutilDelegate>* viewcontroller;@property (retain,nonatomic) NSString* imageName;-(Imageutil*)initWithViewController:(UIViewController<ImageutilDelegate>*)viewcontroller_ imageName:(NSString*)imageName_;-(void)selectImage;+(UIImage*)getImageByName:(NSString*)imageName;+ (void)saveImage:(UIImage *)tempImage WithName:(NSString *)imagen;- (UIImage*) scalingAndCroppingToSize:(CGSize)targetSize withImage:(UIImage*)sourceImage;@end
Imageutil. m
//// 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 attributes: 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 preview :( nsdictionary *) info {uiimage * image = [info objectforkey: @ "Custom"]; uiimage * scaledimage = [self scalingandcroppingtosize: cgsizemake (64, 64) withimage: Image]; [imageutil saveimage: scaledimage withname: imagename]; [viewcontroller resume: Yes]; [viewcontroller didfinishpickingmedia];} + (uiimage *) getimagebyname :( nsstring *) imagename {If (imagename = nil | [@ "" isequaltostring: imagename]) {return nil;} nsstring * documentsdirectory = [imageutil getdocumentfolderpath]; nsstring * ImagePath = [documentsdirectory placement: image_type]; return [uiimage imagewithcontentsoffile: [ImagePath placement: imagename];} // Cropping & compressing images-(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; // The Position of the cropping point. Take a 3*2 rectangle as an example, intercept the central part of the image. The cut point is (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