iOS--UIGraphicsBeginImageContext系列知识(图片缩放裁剪、改。。。

更新时间:2023-07-30 13:41:00 阅读: 评论:0

iOS--UIGraphicsBeginImageContext系列知识(图⽚缩放裁
剪、改。。。
- (UIImage *)rescaleImage:(UIImage *)img ToSize:(CGSize)size; //图⽚缩放裁剪
- (UIImage*)transformWidth:(CGFloat)width height:(CGFloat)height; //改变⼤⼩
+ (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2; //合并图⽚
+ (UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect; //裁剪部分图⽚
+ (void)imageSavedToPhotosAlbum:(UIImage *)image
didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo; //保存图⽚到媒体库
零)重新设置图⽚的尺⼨
- (UIImage *)rescaleImage:(UIImage *)img ToSize:(CGSize)size {
CGRect rect = CGRectMake(0.0, 0.0, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
[img drawInRect:rect]; // scales image to rect
UIImage *resImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resImage;
}
-)根据给定得图⽚,从其指定区域截取⼀张新得图⽚
-(UIImage *)getImageFromImage{
//⼤图bigImage宝宝有痰怎么办
//定义myImageRect,截图的区域
CGRect myImageRect = CGRectMake(10.0, 10.0, 57.0, 57.0);
UIImage* bigImage= [UIImage imageNamed:@"k00030.jpg"];
CGImageRef imageRef = bigImage.CGImage;
CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, myImageRect);
CGSize size;
size.width = 57.0;
size.height = 57.0;
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, myImageRect, subImageRef);
绳中UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
UIGraphicsEndImageContext();
return smallImage;
}
⼆)合并两张图⽚
- (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2 {
UIGraphicsBeginImageContext(image1.size);
// Draw image1
[image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];
土豆茄子青椒// Draw image2
[image2 drawInRect:CGRectMake(0, 0, image2.size.width, image2.size.height)];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultingImage;
}
三)捕捉屏幕截图
CALayer实例使⽤Core Graphics的renderInContext⽅法可以将视图绘制到图像上下⽂中以便转化为其他UIImage实例。前提先#import
+ (UIImage *) imageFromView: (UIView *)theView {
// draw a view's contents into an image context  UIGraphicsBeginImageContext(theView.frame.size);
CGContextRef  context = UIGraphicsGetCurrentContext();
[theView.layer  renderInContext:context];
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
注:UIGraphicsBeginImageContext(CGSize size)创建⼀个基于位图的上下⽂(context),并将其设置为当前上下⽂。函数功能与UIGraphicsBeginImageContextWithOptions相同,相当于该⽅法的opaque参数为NO,scale因⼦为1.0。⽽UIGraphicsEndImageContext()⽅法是移除栈顶的基于当前位图的图形上下⽂。
视图添加倒影效果
const CGFloat kReflectPercent = -0.25f;
const CGFloat kReflectOpacity = 0.3f;
const CGFloat kReflectDistance = 10.0f;
+ (void)addSimpleReflectionToView:(UIView *)theView
阴道美容
{
CALayer *reflectionLayer = [CALayer layer];
reflectionLayer.opacity = kReflectOpacity;
reflectionLayer.frame =
CGRectMake(0.0f,0.0f,theView.frame.size.width,theView.frame.size.height*kReflectPercent);  //倒影层框架设置,其中⾼度是原视图的百分⽐    CATransform3D stransform = CATransform3DMakeScale(1.0f,-1.0f,1.0f);
CATransform3D transform = CATransform3DTranslate(stransform,0.0f,-(kReflectDistance +
theView.frame.size.height),0.0f);
reflectionLayer.sublayerTransform = ansform;
[[theView layer] addSublayer:reflectionLayer];
}
另⼀:使⽤Core Graphics创建倒影
+ (CGImageRef) createGradientImage:(CGSize)size
{
CGFloat colors[] = {0.0,1.0,1.0,1.0};对党章的认识
//在灰⾊设备⾊彩上建⽴⼀渐变    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef context =
CGBitmapContextCreate(nil,size.width,size.height,8,0,colorSpace,kCGImageAlphaNone);
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace,colors,NULL,2);
CGColorSpaceRelea(colorSpace);
//绘制线性渐变    CGPoint p1 = CGPointZero;
CGPoint p2 = CGPointMake(0,size.height);
CGContextDrawLinearGradient(context,gradient,p1,p2,kCGGradientDrawsAfterEndLocation);
//Return the CGImage      CGImageRef theCGImage = CGBitmapContextCreateImage(context);
CFRelea(gradient);
CGContextRelea(context);
return theCGImage;
}
//Create a shrunken frame for the reflection
+ (UIImage *) reflectionOfView:(UIView *)theView WithPercent:(CGFloat) percent
{
//Retain the width but shrink the height    CGSize size = CGSizeMake(theView.frame.size.width, theView.frame.size.height * percent);
//Shrink the View    UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
[theView.layer renderInContext:context];
UIImage *partialimg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//build the mask    CGImageRef mask = [ImageHelper createGradientImage:size];
CGImageRef ref = CGImageCreateWithMask(partialimg.CGImage,mask);
UIImage *theImage = [UIImage imageWithCGImage:ref];
CGImageRelea(ref);
CGImageRelea(mask);
return theImage;
}
const CGFloat kReflectDistance = 10.0f;
+ (void) addReflectionToView: (UIView *)theView
{
theView.clipsToBounds = NO;
UIImageView *reflection = [[UIImageView alloc] initWithImage:[ImageHelper reflectionOfView:theView withPercent:0.45f]];
CGRect frame = reflection.frame;
reflection.frame = frame;
// add the reflection as a simple subview    [theView addSubView:reflection];
[reflection relea];
}
关于图⽚缩放的线程安全和⾮线程安全操作.
⾮线程安全的操作只能在主线程中进⾏操作,对于⼤图⽚的处理肯定会消耗⼤量的时间,如下⾯的⽅法
⽅法 1:使⽤ UIKit
+ (UIImage*)imageWithImage INCLUDEPICTURE
"/Mobile/UploadFiles_9667/201103/20110309123315372.gif" \* MERGEFORMATINET UIImage*)image scaledToSize INCLUDEPICTURE
"/Mobile/UploadFiles_9667/201103/20110309123315372.gif" \* MERGEFORMATINET 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;
}
此⽅法很简单,但是,这种⽅法不是线程安全的情况下.
⽅法 2:使⽤ CoreGraphics
+ (UIImage*)imageWithImage INCLUDEPICTURE
"/Mobile/UploadFiles_9667/201103/20110309123315372.gif" \* MERGEFORMATINET UIImage*)sourceImage scaledToSize INCLUDEPICTURE
"/Mobile/UploadFiles_9667/201103/20110309123315372.gif" \* MERGEFORMATINET CGSize)newSize;
{
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGImageRef imageRef = [sourceImage CGImage];
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);
if (bitmapInfo == kCGImageAlphaNone) {
bitmapInfo = kCGImageAlphaNoneSkipLast;
}
CGContextRef bitmap;
if (sourceImage.imageOrientation == UIImageOrientationUp ||sourceImage.imageOrientation == UIImageOrientationDown) {
多钱善贾bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight,CGImageGetBitsPerCompone
nt(imageRef),CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
} el {
bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth,CGImageGetBitsPerCompone
nt(imageRef),CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
}
if (sourceImage.imageOrientation == UIImageOrientationLeft) {
CGContextRotateCTM (bitmap, radians(90));
CGContextTranslateCTM (bitmap, 0, -targetHeight);
} el if (sourceImage.imageOrientation ==UIImageOrientationRight) {
CGContextRotateCTM (bitmap, radians(-90));
CGContextTranslateCTM (bitmap, -targetWidth, 0);
} el if (sourceImage.imageOrientation == UIImageOrientationUp) {
// NOTHING
} el if (sourceImage.imageOrientation == UIImageOrientationDown){
CGContextTranslateCTM (bitmap, targetWidth, targetHeight);
CGContextRotateCTM (bitmap, radians(-180.));
}
CGContextDrawImage(bitmap, CGRectMake(0, 0, targetWidth,targetHeight), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage* newImage = [UIImage imageWithCGImage:ref];
CGContextRelea(bitmap);
CGImageRelea(ref);
return newImage;
}
这种⽅法的好处是它是线程安全,加上它负责的(使⽤正确的颜⾊空间和位图信息,处理图像⽅向)的⼩东西,UIKit 版本不会。
如何调整和保持长宽⽐(如 AspectFill 选项)?
它是⾮常类似于上述,⽅法,它看起来像这样:

本文发布于:2023-07-30 13:41:00,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1123776.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:线程   位图   倒影   截图   长宽   处理
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图