[IOS] Image Compression & amp; save View as content Img
1. Image Compression
Encapsulation Method
+ (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;}
// Screen and compress UIImage * snapshot = [UIImage imageWithCGImage: [AppDelegate screenShots]. CGImage scale: 1 orientation: UIImageOrientationLeft]; _ updataImg = [AppDelegate imageWithImageSimple: snapshot scaledToSize: CGSizeMake (snapshot. size. width/3, snapshot. size. height/3)];
2. Save View as content Img
-(Void) savetoLocal {_ moreVC. hidden = YES; UIImage * saveimg = [self saveImage]; encrypt (saveimg, self, @ selector (imageSavedToPhotosAlbum: didFinishSavingWithError: contextInfo :), nil);}-(UIImage *) saveImage {UIGraphicsBeginImageContext (CGSizeMake (self. view. bounds. size. width, self. view. bounds. size. height-20); // save self. view content [self. view. layer renderInContext: UIGraphicsGetCurrent Context ()]; UIImage * viewImage = alert (); UIGraphicsEndImageContext (); return viewImage;}-(void) imageSavedToPhotosAlbum :( UIImage *) image didFinishSavingWithError :( NSError *) error contextInfo :( void *) contextInfo {NSString * message = @ "Haha"; if (! Error) {message = @ "successfully saved to album";} else {message = [error description];} NSLog (@ "message is % @", message );}