Pragmatism, directly on the code, Welcome to take away!
1, compression of pictures
- (UIImage*) Scalefromimage: (UIImage*) Image scaledtosize: (cgsize) newsize{cgsizeImageSize = Image. Size;CGFloatwidth = imageSize. Width;CGFloatHeight = imageSize. Height;if(Width <= newSize. Width&& Height <= newSize. Height){returnImage }if(Width = =0|| Height = =0){returnImage }CGFloatWidthfactor = NewSize. Width/width;CGFloatHeightfactor = NewSize. Height/height;CGFloatScalefactor = (widthfactorCGFloatScaledwidth = width * scalefactor;CGFloatScaledheight = height * scalefactor;cgsizeTargetsize = Cgsizemake (scaledwidth,scaledheight); Uigraphicsbeginimagecontext (targetsize); [Image Drawinrect:cgrectmake (0,0, Scaledwidth,scaledheight)];UIImage* NewImage = Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext ();returnNewImage;}
Convert to NSData and compress
The SDK provides methods that can be directly called UIImage *img = [UIImage imagenamed:@ "Some.png"];NSData *dataobj = UIImageJPEGRepresentation (IMG,1.0); The following is the contents of the UIImage.h header file in the SDK Uikit_extern NSData *uiimagepngrepresentation (UIImage *image); //returnImage asPng. MayreturnNilifImage has no cgimagereforInvalid bitmap format Uikit_extern nsdata *uiimagejpegrepresentation (UIImage *image, CGFloat compressionquality); //returnImage asJpeg. MayreturnNilifImage has no cgimagereforInvalid bitmap format. Compression is 0(most).. 1(least) JPEG conversion method inside the second parameter is the compression coefficient, can effectively reduce the size of the picture. Uiimagepngrepresentation (uiimage* image) is more than uiimagejpegrepresentation (uiimage* image,1.0The amount of picture data returned is much larger. Before uploading pictures in the project, after testing the same photo taken the PNG size in8M, while the JPG compression factor is0.75Time, size only1M. Also, reducing the compression factor does not have much effect on the picture visually.
2, the interception of pictures
. h
#import <UIKit/UIKit.h> @protocol imagecropperdelegate; @interface imagecropper : uiviewcontroller <uiscrollviewdelegate> { Uiscrollview*scrollview;Uiimageview*imageview;ID<ImageCropperDelegate> delegate;}@property(nonatomic, retain)Uiscrollview*scrollview;@property(nonatomic, retain)Uiimageview*imageview;@property(nonatomic,Assign)ID<ImageCropperDelegate> delegate;-(ID) Initwithimage: (UIImage*) image;@end @protocol imagecropperdelegate <nsobject>- (void) Imagecropper: (Imagecropper *) Cropper Didfinishcroppingwithimage: (UIImage*) image;-(void) Imagecropperdidcancel: (Imagecropper *) cropper;@end
. m
#import "ImageCropper.h" @implementation imagecropper @synthesizeScrollView, ImageView;@synthesizedelegate;-(ID) Initwithimage: (UIImage*) Image { Self= [SuperINIT];if( Self) { [[uiapplicationSharedapplication] Setstatusbarstyle:uistatusbarstyleblacktranslucent Animated:YES]; ScrollView = [[UiscrollviewAlloc] Initwithframe:cgrectmake (0.0, -20.0,320.0,480.0)]; [ScrollView setbackgroundcolor:[UicolorBlackcolor]]; [ScrollView setdelegate: Self]; [ScrollView Setshowshorizontalscrollindicator:NO]; [ScrollView Setshowsverticalscrollindicator:NO]; [ScrollView Setmaximumzoomscale:2.0]; ImageView = [[UiimageviewAlloc] initwithimage:image];CGRectRect Rect. Size. Width= Image. Size. Width; Rect. Size. Height= Image. Size. Height; [ImageView Setframe:rect]; [ScrollView Setcontentsize:[imageview Frame]. Size]; [ScrollView Setminimumzoomscale:[scrollview Frame]. Size. Width/[ImageView frame]. Size. Width]; [ScrollView Setzoomscale:[scrollview Minimumzoomscale]; [ScrollView Addsubview:imageview]; [[ SelfView] Addsubview:scrollview];Uinavigationbar*navigationbar = [[UinavigationbarAlloc] Initwithframe:cgrectmake (0.0,0.0,320.0,44.0)]; [Navigationbar Setbarstyle:uibarstyleblack]; [Navigationbar settranslucent:YES]; Uinavigationitem *anavigationitem = [[Uinavigationitem alloc] initwithtitle:@"Move and scale"]; [Anavigationitem setleftbarbuttonitem:[[[UibarbuttonitemAlloc] Initwithbarbuttonsystemitem:uibarbuttonsystemitemcancel target: SelfAction@selector(cancelcropping)] Autorelease]]; [Anavigationitem setrightbarbuttonitem:[[[UibarbuttonitemAlloc] Initwithbarbuttonsystemitem:uibarbuttonsystemitemdone target: SelfAction@selector(finishcropping)] Autorelease]]; [Navigationbar setitems:[NsarrayArraywithobject:anavigationitem]]; [Anavigationitem release]; [[ SelfView] Addsubview:navigationbar]; [Navigationbar release]; }return Self;} - (void) cancelcropping {[Delegate imagecropperdidcancel: Self]; }- (void) Finishcropping {floatZoomscale =1.0/[ScrollView Zoomscale];CGRectRect Rect. Origin. x= [ScrollView Contentoffset]. x* Zoomscale; Rect. Origin. Y= [ScrollView Contentoffset]. Y* Zoomscale; Rect. Size. Width= [ScrollView bounds]. Size. Width* Zoomscale; Rect. Size. Height= [ScrollView bounds]. Size. Height* Zoomscale; Cgimageref cr = Cgimagecreatewithimageinrect ([[[[ImageView Image] cgimage], rect);UIImage*cropped = [UIImageIMAGEWITHCGIMAGE:CR]; Cgimagerelease (CR); [Delegate Imagecropper: SelfDidfinishcroppingwithimage:cropped];} - (UIView*) Viewforzoominginscrollview: (Uiscrollview*) ScrollView {returnImageView;} - (void) Dealloc {[ImageView release]; [ScrollView release]; [SuperDealloc];}@end
Uiiimage and compression of pictures