iOS UIImage各屬性 & 圖片顛倒

UIImage 的方向問題:
拍照,或者從相冊中選擇照片,進行剪切,然后分享.結果出現(xiàn)了,剪切后圖片顛倒或者旋轉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 // 根據(jù)圖片的使用環(huán)境和所處的繪圖上下文自動調整渲染模式。
UIImageRenderingModeAlwaysOriginal // 始終繪制圖片原始狀態(tài),不使用Tint Color。
UIImageRenderingModeAlwaysTemplate // 始終根據(jù)Tint Color繪制圖片,忽略圖片的顏色信息。

1.UIImageRenderingModeAlwaysOriginal:在navigationBar和tabbar上,使用系統(tǒng)控件加載圖片時,圖片會顯示成藍色。因此,你應該這樣改寫你的代碼:

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”,這樣可以形成動態(tài)圖片,capInsets:是保持圖片某方向一定寬度不變形的設置屬性,例如,right的50寬度就是保證圖片左右不變形,但是上下就會變形(不在top和bottom的50寬度內)。

四. drawAtPoint繪圖:在方法:- (void)drawRect:(CGRect)rect {}中進行調用,進行相應的繪圖。

五. resizableImageWithCapInsets:進行單個圖片的大小調整,根據(jù)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), 圖片的兩種格式。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容