图片旋转加图上圆角

    在做通讯录页面的时候,头像需要圆角显示,使用layer的方式虽然也可以实现但是会比较卡。所以找了另外一种方法,但是也有个问题是,用户横着手机拍照的照片做头像的话,会被自动旋转,虽然也有相应的方法解决,但是太慢了,旋转一张图片需要1.2秒左右。不知道有没有高手可以解决这个问题。另外想到一种办法,但是还没试的,就是在gcd里面使用layer的方法给UImageView画圆角,不知道这样是否有风险。

UIView加圆角会卡的解决办法

做iOS开发的时候有可能经常碰到莫名其妙的滑动不顺畅(在UIScrollView以及其子类)或者在navigation切换的时候不顺畅的问题,这很有可能是在界面中加了太多带圆角并且masksToBounds为YES的UIView而引起的。 

暂时所有版本的api都没有解决这个问题。因此如果想要避免这个问题,只能放弃在UIView上加圆角,如果该View是用来显示图片的,则可以将该UIView上的图片本身加圆角。 

  1. static void addRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth,  
                                     float ovalHeight)  
    {  
        float fw, fh;  
        if (ovalWidth == 0 || ovalHeight == 0) {  
            CGContextAddRect(context, rect);  
            return;  
        }  
        CGContextSaveGState(context);  
        CGContextTranslateCTM(context, CGRectGetMinX(rect), CGRectGetMinY(rect));  
        CGContextScaleCTM(context, ovalWidth, ovalHeight);  
        fw = CGRectGetWidth(rect) / ovalWidth;  
        fh = CGRectGetHeight(rect) / ovalHeight;  
        CGContextMoveToPoint(context, fw, fh/2);  // Start at lower right corner  
        CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);  // Top right corner  
        CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1); // Top left corner  
        CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1); // Lower left corner  
        CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); // Back to lower right  
        CGContextClosePath(context);  
        CGContextRestoreGState(context);  
    }  
      
    + (id) createRoundedRectImage:(UIImage*)image size:(CGSize)size  
    {  
        // the size of CGContextRef  
        int w = size.width;  
        int h = size.height;  
          
        UIImage *img = image;  
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();  
        CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);  
        CGRect rect = CGRectMake(0, 0, w, h);  
        CGContextBeginPath(context);  
        addRoundedRectToPath(context, rect, 10, 10);  
        CGContextClosePath(context);  
        CGContextClip(context);  
        CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);  
        CGImageRef imageMasked = CGBitmapContextCreateImage(context);  
        CGContextRelease(context);  
        CGColorSpaceRelease(colorSpace);  
        return [UIImage imageWithCGImage:imageMasked];  
    }  

将UIView上的图片用该方法处理一次,就可以获得带了圆角的图片了。


解决UIImage图片旋转

iOS程序中使用系统相机拍照和从相册选取图片,直接上传后在非mac系统下看到的图片会发生旋转的现象,那是因为我们没有通过图片的旋转属性修改图片倒置的。下面的方法可以很简单的解决旋转问题:

@interface UIImage (fixOrientation)
   
- (UIImage *)fixOrientation;
   
@end
   
   
   
@implementation UIImage (fixOrientation)
   
- (UIImage *)fixOrientation {
   
    // No-op if the orientation is already correct
    if (self.imageOrientation == UIImageOrientationUp) return self;
   
    // 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 (self.imageOrientation) {
        case UIImageOrientationDown:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;
   
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
            transform = CGAffineTransformTranslate(transform, self.size.width, 0);
            transform = CGAffineTransformRotate(transform, M_PI_2);
            break;
   
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, 0, self.size.height);
            transform = CGAffineTransformRotate(transform, -M_PI_2);
            break;
    }
   
    switch (self.imageOrientation) {
        case UIImageOrientationUpMirrored:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, self.size.width, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
   
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, self.size.height, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
    }
   
    // Now we draw the underlying CGImage into a new context, applying the transform
    // calculated above.
    CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height,
                                             CGImageGetBitsPerComponent(self.CGImage), 0,
                                             CGImageGetColorSpace(self.CGImage),
                                             CGImageGetBitmapInfo(self.CGImage));
    CGContextConcatCTM(ctx, transform);
    switch (self.imageOrientation) {
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            // Grr...
            CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage);
            break;
   
        default:
            CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.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;
}
   
@end




你可能感兴趣的:(uiimage,图片圆角,图片旋转)