ios 图片压缩

来源:互联网 发布:淘宝男潮鞋店铺推荐 编辑:程序博客网 时间:2024/04/28 13:06

在ios的开发中,会经常遇到处理图片的问题,所以我提供了一个可以根据比例、大小进行压缩图片的方法,代码如下

A,//传入的参数:1、生成图片的大小 2、压缩比 3、存放图片的路径

+ (void)createThumbImage:(UIImage *)image size:(CGSize )thumbSize percent:(float)percent toPath:(NSString *)thumbPath{

        CGSize imageSize = image.size;

     CGFloat width = imageSize.width;

     CGFloat height = imageSize.height;

        CGFloat scaleFactor = 0.0;

     CGPoint thumbPoint = CGPointMake(0.0,0.0);

     CGFloat widthFactor = thumbSize.width / width;

     CGFloat heightFactor = thumbSize.height / height;

     if (widthFactor > heightFactor)  {

       scaleFactor = widthFactor;

     }

     else {

       scaleFactor = heightFactor;

     }

     CGFloat scaledWidth  = width * scaleFactor;

     CGFloat scaledHeight = height * scaleFactor;

     if (widthFactor > heightFactor)

     {

       thumbPoint.y = (thumbSize.height - scaledHeight) * 0.5; 

     }

     else if (widthFactor < heightFactor)

     {

       thumbPoint.x = (thumbSize.width - scaledWidth) * 0.5;

     }

      UIGraphicsBeginImageContext(thumbSize);

     CGRect thumbRect = CGRectZero;

     thumbRect.origin = thumbPoint;

     thumbRect.size.width  = scaledWidth;

     thumbRect.size.height = scaledHeight;

     [image drawInRect:thumbRect];

   

     UIImage *thumbImage = UIGraphicsGetImageFromCurrentImageContext();

     UIGraphicsEndImageContext();

    NSData *thumbImageData = UIImageJPEGRepresentation(thumbImage, percent);

     [thumbImageData writeToFile:thumbPath atomically:NO];

}

B,下面的这个方法适用于 对压缩后的图片的质量要求不高或者没有要求,因为这种方法只是压缩了图片的大小

+(UIImage *)scale:(UIImage *)image toSize:(CGSize)size

{

    UIGraphicsBeginImageContext(size);

    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];

    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return scaledImage;

}

C,

//压缩图片

+ (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize

{

    // Create a graphics image context

    UIGraphicsBeginImageContext(newSize);

    // Tell the old image to draw in this new context, with the desired

    // new size

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

    // Get the new image from the context

    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

    // End the context

    UIGraphicsEndImageContext();

    // Return the new image.

    return newImage;

}

 

cell.typeImageView.imageURL = [NSURL URLWithString:@"http://qq.5068.com/uploads/allimg/130107/111Qa446-13.jpg"];

    NSData  *data = UIImagePNGRepresentation(cell.typeImageView.image);

    NSLog(@"--==== data ==  %d", data.length);

    cell.typeImageView.image = [UnityClass imageWithImageSimple:cell.typeImageView.image scaledToSize:CGSizeMake(80, 80)];

    NSData  *data1 = UIImagePNGRepresentation(cell.typeImageView.image);

    NSLog(@"--==== data1 ==  %d", data1.length);

通过打印NSData压缩前后的长度来看是否压缩.


这段“许靖昕”先生分享的代码将示范如何缩小 UIImage

    .    @implementation UIImage (Extras) 

    .    #pragma mark 

    .    #pragma mark Scale and crop image 

    .    - (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize 

    .    { 

    .    UIImage *sourceImage self; 

    .    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; // scale to fit height 

    .            else 

    .                    scaleFactor heightFactor; // scale to fit width 

    .            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; 

    .                            

    .                   

    .    UIGraphicsBeginImageContext(targetSize); // this will crop 

    .    CGRect thumbnailRect CGRectZero; 

    .    thumbnailRect.origin thumbnailPoint; 

    .    thumbnailRect.size.width  scaledWidth; 

    .    thumbnailRect.size.height scaledHeight; 

    .    [sourceImage drawInRect:thumbnailRect]; 

    .    newImage UIGraphicsGetImageFromCurrentImageContext(); 

    .    if(newImage == nil)  

    .            NSLog(@"could not scale image"); 

    .    //pop the context to get back to the default 

    .    UIGraphicsEndImageContext(); 

    .    return newImage; 

    .    } 


最简单的加载网络图片的方法:

用NSData。

NSString* URL = @"http://www.cocoachina.com/bbs/images/post/smile/default/13.gif";

NSData* imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:mapURL]];

UIImage* image = [[UIImage alloc] initWithData:imageData];

[imageView setImage:image];

[imageData release];

[image release]; 

0 0
原创粉丝点击