iOS 图片的处理小结

来源:互联网 发布:ubuntu ntp设置 编辑:程序博客网 时间:2024/06/05 08:54

一:图片的合成


二:图片分割,获取指定区域的大小

- (UIImage *)clipImage: (UIImage *)image inRect: (CGRect) rect

{    

    //返回image中rect范围内的图片

    CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, rect);

    UIImage *subImage = [UIImage imageWithCGImage:imageRef];

    return subImage;


}

三:图片压缩的处理方式

 1:UIImageJPEGRepresentation(image, 0.3);方法Iphone上有两种读取图片数据的简单方法: UIImageJPEGRepresentationUIImagePNGRepresentation. UIImageJPEGRepresentation函数需要两个参数:图片的引用和压缩系数.UIImagePNGRepresentation只需要图片引用作为参数.通过在实际使用过程中,比较发现: UIImagePNGRepresentation(UIImage* image) 要比UIImageJPEGRepresentation(UIImage* image, 1.0) 返回的图片数据量大很多.譬如,同样是读取摄像头拍摄的同样景色的照片, UIImagePNGRepresentation()返回的数据量大小为199K , UIImageJPEGRepresentation(UIImage* image, 1.0)返回的数据量大小只为140KB,比前者少了50KB.如果对图片的清晰度要求不高,还可以通过设置 UIImageJPEGRepresentation函数的第二个参数,大幅度降低图片数据量.譬如,刚才拍摄的图片,通过调用UIImageJPEGRepresentation(UIImage* image, 1.0)读取数据时,返回的数据大小为140KB,但更改压缩系数后,通过调用UIImageJPEGRepresentation(UIImage* image, 0.5)读取数据时,返回的数据大小只有11KB,大大压缩了图片的数据量 ,而且从视角角度看,图片的质量并没有明显的降低.因此,在读取图片数据内容时,建议优先使用UIImageJPEGRepresentation,并可根据自己的实际使用场景,设置压缩系数,进一步降低图片数据量大小。

  

//2:压缩成指定大小的图片

-(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize


{

    // 创建一个bitmapcontext,并把它设置成为当前正在使用的context

    UIGraphicsBeginImageContext(newSize);

    //绘制图片

    [imagedrawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

    //获取图片

    UIImage* newImage =UIGraphicsGetImageFromCurrentImageContext();

    

    UIGraphicsEndImageContext();


    //返回新的改变大小后的图片

   return newImage;

    

}


//3:等比例压缩图片

-(UIImage *) imageCompressForSize:(UIImage *)sourceImage targetSize:(CGSize)size{

    

   UIImage *newImage =nil;

   CGSize imageSize = sourceImage.size;

   CGFloat width = imageSize.width;

   CGFloat height = imageSize.height;

   CGFloat targetWidth = size.width;

   CGFloat targetHeight = size.height;

   CGFloat scaleFactor =0.0; CGFloat scaledWidth = targetWidth;

   CGFloat scaledHeight = targetHeight;

   CGPoint thumbnailPoint =CGPointMake(0.0,0.0);

    

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

       CGFloat widthFactor = targetWidth / width;

       CGFloat heightFactor = targetHeight / height;

       if(widthFactor > heightFactor){

            scaleFactor = widthFactor;

        }else{

            scaleFactor = heightFactor;

        }

        scaledWidth = width * scaleFactor;

        scaledHeight = height * scaleFactor;

       if(widthFactor > heightFactor){

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

        }elseif(widthFactor < heightFactor){

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

        } }

    

    UIGraphicsBeginImageContext(size);

   CGRect thumbnailRect =CGRectZero;

    thumbnailRect.origin = thumbnailPoint;

    thumbnailRect.size.width = scaledWidth;

    thumbnailRect.size.height = scaledHeight;

    [sourceImagedrawInRect:thumbnailRect];

    newImage = UIGraphicsGetImageFromCurrentImageContext();

   if(newImage ==nil){

        NSLog(@"scale image fail");

    }

    UIGraphicsEndImageContext();

   return newImage;

}


四:颜色值转换成图片


- (UIImage*) createImageWithColor: (UIColor*) color

{

    //大小自定义

   CGRect rect=CGRectMake(0.0f,0.0f, 100.0f,10.0f);

    UIGraphicsBeginImageContext(rect.size);

    CGContextRef context =UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [colorCGColor]);

   CGContextFillRect(context, rect);

    UIImage *image =UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    

    

   return image;

}



0 0
原创粉丝点击