UIImage 的方向问题:
拍照,或者从相册中选择照片,进行剪切,然后分享.结果出现了,剪切后图片颠倒或者旋转90度的问题.
可以运用imageOrientation这个属性.
{
UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(90, 180, 50, 50)];
[self.view addSubview:imageV];
UIImage *image = [UIImage imageNamed:@"task_chargeLay"];
// imageV.image = image;
/**
UIImageOrientationUp, // default orientation
UIImageOrientationDown, // 180 deg rotation
UIImageOrientationLeft, // 90 deg CCW
UIImageOrientationRight, // 90 deg CW
UIImageOrientationUpMirrored, // as above but image mirrored along other axis. horizontal flip
UIImageOrientationDownMirrored, // horizontal flip
UIImageOrientationLeftMirrored, // vertical flip
UIImageOrientationRightMirrored,
*/
CGAffineTransform transform = CGAffineTransformIdentity;
// transform = CGAffineTransformTranslate(transform, image.size.width, image.size.height);
transform = CGAffineTransformRotate(transform, - M_PI_4);
switch (image.imageOrientation) {
case UIImageOrientationUp:
{
NSLog(@"UIImageOrientationUp");
}
break;
case UIImageOrientationDown:
{
NSLog(@"UIImageOrientationDown");
}
break;
case UIImageOrientationLeft:
{
NSLog(@"UIImageOrientationLeft");
}
break;
case UIImageOrientationRight:
{
NSLog(@"UIImageOrientationRight");
}
break;
default:
break;
}
CGContextRef ctx = CGBitmapContextCreate(NULL, image.size.width, image.size.height,
CGImageGetBitsPerComponent(image.CGImage), 0,
CGImageGetColorSpace(image.CGImage),
CGImageGetBitmapInfo(image.CGImage));
CGContextConcatCTM(ctx, transform);
switch (image.imageOrientation) {
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
// Grr...
CGContextDrawImage(ctx, CGRectMake(0,0,image.size.height,image.size.width), image.CGImage);
break;
default:
CGContextDrawImage(ctx, CGRectMake(0,0,image.size.width,image.size.height), image.CGImage);
break;
}
// And now we just create a new UIImage from the drawing context
CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
UIImage *img = [UIImage imageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);
imageV.image = img;
UIImage其他属性:
一. typedef NS_ENUM(NSInteger, UIImageResizingMode) {
UIImageResizingModeTile, //拼接平铺
UIImageResizingModeStretch, //拉伸,延展
};
二. UIImageRenderingMode:
设置UIImage的渲染模式:UIImage.renderingMode,着色(Tint Color)是iOS7界面中的一个设置。
UIImageRenderingModeAutomatic // 根据图片的使用环境和所处的绘图上下文自动调整渲染模式。
UIImageRenderingModeAlwaysOriginal // 始终绘制图片原始状态,不使用Tint Color。
UIImageRenderingModeAlwaysTemplate // 始终根据Tint Color绘制图片,忽略图片的颜色信息。
1.UIImageRenderingModeAlwaysOriginal:在navigationBar和tabbar上,使用系统控件加载图片时,图片会显示成蓝色。因此,你应该这样改写你的代码:
yourController.tabBarItem.image = [[UIImage imageNamed:@"tabbar_image_2"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
yourController.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_HLimage_2"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
2.UIImageRenderingModeAlwaysTemplate 如果你要用代码改变图片颜色(一般对于单一颜色的图片),可以这样写
UIImage *theImage = [UIImage imageNamed:@"123.png"];
theImage = [theImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
imageView.image = theImage;
imageView.tintColor = [UIColor blueColor];
三. 以animated开头的方法,是制作动画。--animatedResizableImageNamed:...此方法,例如:
UIImage *image = [UIImage animatedResizableImageNamed:@"test" capInsets:UIEdgeInsetsMake(50, 50, 50, 50) resizingMode:(UIImageResizingModeStretch) duration:2];
文件中图片名有“test0” “ test1” “ test2”,这样可以形成动态图片,capInsets:是保持图片某方向一定宽度不变形的设置属性,例如,right的50宽度就是保证图片左右不变形,但是上下就会变形(不在top和bottom的50宽度内)。
四. drawAtPoint绘图:在方法:- (void)drawRect:(CGRect)rect {}中进行调用,进行相应的绘图。
五. resizableImageWithCapInsets:进行单个图片的大小调整,根据model的不用,平铺或者拉伸排列,capinset:维持不变的部分。
六. UIKIT_EXTERN NSData * __nullable UIImagePNGRepresentation(UIImage * __nonnull image); // return image as PNG. May return nil if image has no CGImageRef or invalid bitmap format
UIKIT_EXTERN NSData * __nullable UIImageJPEGRepresentation(UIImage * __nonnull image, CGFloat compressionQuality); // return image as JPEG. May return nil if image has no CGImageRef or invalid bitmap format. compression is 0(most)..1(least), 图片的两种格式。