zoukankan      html  css  js  c++  java
  • UIImageView 动画 / UIImage 方向

    UIImage 方向

    UIImage imageOrientation是相对当前屏幕的横竖屏来判断方向

    如果本身是横屏, 照片也是横屏的话, 方向是正方向

            

            

    BOOL b1 = (originalImage.imageOrientation == UIImageOrientationUp || originalImage.imageOrientation == UIImageOrientationDown);
    BOOL b2 = (originalImage.imageOrientation == UIImageOrientationLeft || originalImage.imageOrientation == UIImageOrientationRight);
    BOOL b3 = originalImage.imageOrientation == UIImageOrientationLeft;
    BOOL b4 = originalImage.imageOrientation == UIImageOrientationRight;

    横屏中, 正面照是:UIImageOrientationRight

    upsidedown 照片是 UIImageOrientationLeft

    //根据给定得图片,从其指定区域截取一张新得图片
    -(UIImage *)getImageFromImage:(UIImage *)image rect:(CGRect)rect
    {
        //开始上下文
        UIGraphicsBeginImageContext(rect.size);
        CGImageRef imageRef = image.CGImage;
        //创建子图像索引
        CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, rect);
        //获取当前上下文
        CGContextRef context = UIGraphicsGetCurrentContext();
        
        //在当前上下文下, 定位为rect下, 画出子图像索引
        CGContextDrawImage(context,CGRectMake(0, 0, rect.size.width, rect.size.height), subImageRef);
        
        //创建图片对象, 根据图像索引
        UIImage *subImage = [[UIImage alloc] initWithCGImage:subImageRef];
        
        //销毁创建的子图像索引,不然会导致内存泄露
        CGImageRelease(subImageRef);
        //结束上下文
        UIGraphicsEndImageContext();
        return subImage;
    }

    被render成为新图片之后 uiimage默认的imageOrientation是正方向(UIImageOrientationUp)

    图片方向转换

    这几天写个拍照,或者从相册中选择照片,进行剪切,然后分享.结果出现了,剪切后图片颠倒或者旋转90度的问题.
    
    找了很久才发现是忽略imageOrientation这个属性.
    
    以下为解决方法:
    
    
    [cpp] view plaincopy
    + (UIImage *)fixOrientation:(UIImage *)aImage {  
          
        // No-op if the orientation is already correct  
        if (aImage.imageOrientation == UIImageOrientationUp)   
            return aImage;  
          
        // We need to calculate the proper transformation to make the image upright.  
        // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.  
        CGAffineTransform transform = CGAffineTransformIdentity;  
          
        switch (aImage.imageOrientation) {  
            case UIImageOrientationDown:  
            case UIImageOrientationDownMirrored:  
                transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);  
                transform = CGAffineTransformRotate(transform, M_PI);  
                break;  
                  
            case UIImageOrientationLeft:  
            case UIImageOrientationLeftMirrored:  
                transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);  
                transform = CGAffineTransformRotate(transform, M_PI_2);  
                break;  
                  
            case UIImageOrientationRight:  
            case UIImageOrientationRightMirrored:  
                transform = CGAffineTransformTranslate(transform, 0, aImage.size.height);  
                transform = CGAffineTransformRotate(transform, -M_PI_2);  
                break;  
            default:  
                break;  
        }  
          
        switch (aImage.imageOrientation) {  
            case UIImageOrientationUpMirrored:  
            case UIImageOrientationDownMirrored:  
                transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);  
                transform = CGAffineTransformScale(transform, -1, 1);  
                break;  
                  
            case UIImageOrientationLeftMirrored:  
            case UIImageOrientationRightMirrored:  
                transform = CGAffineTransformTranslate(transform, aImage.size.height, 0);  
                transform = CGAffineTransformScale(transform, -1, 1);  
                break;  
            default:  
                break;  
        }  
          
        // Now we draw the underlying CGImage into a new context, applying the transform  
        // calculated above.  
        CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height,  
                                                 CGImageGetBitsPerComponent(aImage.CGImage), 0,  
                                                 CGImageGetColorSpace(aImage.CGImage),  
                                                 CGImageGetBitmapInfo(aImage.CGImage));  
        CGContextConcatCTM(ctx, transform);  
        switch (aImage.imageOrientation) {  
            case UIImageOrientationLeft:  
            case UIImageOrientationLeftMirrored:  
            case UIImageOrientationRight:  
            case UIImageOrientationRightMirrored:  
                // Grr...  
                CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage);  
                break;  
                  
            default:  
                CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.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);  
        return img;  
    }  

    来源:http://blog.csdn.net/iukey/article/details/7308433

    使用UIImageView来播放动画.

    -(void)viewDidLoad
    {
    	[superviewDidLoad];
    
    	self.title=NSLocalizedString(@"ImagesTitle",@"");
    	//setupourUIImagewithagrouporarrayofimagestoanimate(orinourcaseaslideshow)	
    	self.imageView.animationImages = [NSArrayarrayWithObjects:[UIImageimageNamed:@"scene1.jpg"],[UIImageimageNamed:@"scene2.jpg"],[UIImageimageNamed:@"scene3.jpg"],[UIImageimageNamed:@"scene4.jpg"],[UIImageimageNamed:@"scene5.jpg"],  nil  ];
    
    	imageView.animationDuration=5.0;
    	[self.imageViewstopAnimating];
    
    	//Settheappropriateaccessibilitylabels.
    	[self.imageViewsetIsAccessibilityElement:YES];
    	[self.imageViewsetAccessibilityLabel:self.title];
    	[self.slidersetAccessibilityLabel:NSLocalizedString(@"DurationSlider",@"")];
    }
  • 相关阅读:
    日期验证正则表达式
    删除数据表中重复的记录
    转:精选15个国外CSS框架
    WEB打印大全(转)
    document.execCommand()方法使用的语法
    Jquery 1000 demo
    完成 ajax.net 的cross domain ajax功能实现
    "方案改进:直接通过User Control生成HTML", 我的改进意见
    实现Mashup的基本技术手段: cross domain ajax
    你还在用GUID作数据库表的主键吗?
  • 原文地址:https://www.cnblogs.com/apem/p/4421954.html
Copyright © 2011-2022 走看看