图片处理

来源:互联网 发布:软件测试兼职网站 编辑:程序博客网 时间:2024/05/21 05:19

关于图片适应情况的处理

1、使用uiview  属性

    contentMode


   凡是没有带Scale的,当图片尺寸超过 ImageView尺寸时,只有部分显示在ImageView中。UIViewContentModeScaleToFill属性会导致图片变形。UIViewContentModeScaleAspectFit会保证图片比例不变,而且全部显示在ImageView中,这意味着ImageView会有部分空白。UIViewContentModeScaleAspectFill也会证图片比例不变,但是是填充整个ImageView的,可能只有部分图片显示出来。

  官方解释:

    内容模式设置啦当view的大小变化的时候,view包含的位图图片如何自动适应。这个属性经常用来重新设置,通常和contentStretch 一起设置。代替每次都要重绘视图。

The content mode specifies how the cached bitmap of the view’s layer is adjusted when the view’s bounds change. This property is often used to implement resizable controls, usually in conjunction with the contentStretch property. Instead of redrawing the contents of the view every time, you can use this property to specify that you want to scale the contents (either with or without distortion) or pin them to a particular spot on the view.

2、使用uiimage方法

                 

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode NS_AVAILABLE_IOS(6_0); // the interior is resized according to the resizingMode  


3、对UIimage进行绘图处理

     适用于处理图片大小适应问题,优点:减少写入磁盘的大小,和读取到内存中得大小;

+(UIImage *)imageByScalingToSize:(CGSize)targetSize image:(UIImage *)image

{

    

    UIImage *sourceImage =image;

    

    UIImage *newImage = nil;

    

    CGSize imageSize = sourceImage.size;

    

    CGFloat width = imageSize.width;

    

    CGFloat height = imageSize.height;

    

    CGFloat targetWidth = targetSize.width;

    

    CGFloat targetHeight = targetSize.height;

    

    CGFloat scaleFactor = 0.0;

    

    CGFloat scaledWidth = targetWidth;

    

    CGFloat scaledHeight = targetHeight;

    

    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);

    

    if (CGSizeEqualToSize(imageSize, targetSize) == NO) {

        

        CGFloat widthFactor = targetWidth / width;

        

        CGFloat heightFactor = targetHeight / height;

        

        if (widthFactor < heightFactor)

            

            scaleFactor = widthFactor;

        

        else

            

            scaleFactor = heightFactor;

        

        scaledWidth  = width * scaleFactor;

        

        scaledHeight = height * scaleFactor;

        

        // center the image

        

        if (widthFactor < heightFactor) {

            

            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;

            

        } else if (widthFactor > heightFactor) {

            

            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;

            

        }

        

    }

    

    // this is actually the interesting part:

    

    UIGraphicsBeginImageContext(targetSize);

    

    CGRect thumbnailRect = CGRectZero;

    

    thumbnailRect.origin = thumbnailPoint;

    

    thumbnailRect.size.width  = scaledWidth;

    

    thumbnailRect.size.height = scaledHeight;

    

    [sourceImage drawInRect:thumbnailRect];

    

    newImage = UIGraphicsGetImageFromCurrentImageContext(); 

    

    UIGraphicsEndImageContext(); 

    

    if(newImage == nil

        

        NSLog(@"could not scale image");   

    

    return newImage ; 

    

}


转载:http://blog.csdn.net/denghuihua/article/details/20377207
0 0
原创粉丝点击