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), 圖片的兩種格式。