// 尺寸變換
-
(UIImage *)reSizeImage:(UIImage *)image toSize:(CGSize)reSize {
// 創(chuàng)建圖形要變化的大小的上下文
UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height));
// 繪制圖形的位置和大小
[image drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)];
// 圖形獲取圖片從當(dāng)前圖片的上下文
UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
// 結(jié)束圖形繪制
UIGraphicsEndImageContext();return reSizeImage;
}
// 等比率縮放
- (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize{
UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize));
[image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
// 截圖
-
(UIImage *)captureView:(UIView *)view {
// 獲得截圖view的fram
CGRect rect = view.frame;
// 圖形開始圖片的上下文
UIGraphicsBeginImageContext(rect.size);
// 獲得當(dāng)前上下文
CGContextRef context = UIGraphicsGetCurrentContext();
// 呈現(xiàn)圖形當(dāng)前的上下文到目標(biāo)view的layer層中
[view.layer renderInContext:context];UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
// 儲存圖片扳抽, 這里分為儲存在app的文件里和儲存到手機的圖庫里
// 1.儲存到app的文件里
- (void)saveImageToAppFileWithImage:(UIImage *)image path:(NSString *)path{
//把要處理的圖片, 以image.png名稱存到app home下的Documents目錄里
// NSString *pathStr = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"image.png"];
[UIImagePNGRepresentation(image) writeToFile:path atomically:YES];
}
// 2.儲存圖片到手機相冊中(必須真機使用,模擬器無法使用)
-
(void)saveImageToPhotoAlbum:(UIImage*)savedImage {
UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}
// 保存圖片成功與否的提示
- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo
{
NSString *msg = nil ;
if(error != NULL){
msg = @"保存圖片失敗" ;
}else{
msg = @"保存圖片成功" ;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"保存圖片結(jié)果提示"
message:msg
delegate:self
cancelButtonTitle:@"確定"
otherButtonTitles:nil];
[alert show];
}