图像压缩

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
- (UIImage*)scaleFromImage:(UIImage*)image scaledToSize :(CGSize)newSize
{
  CGSize imageSize = image .size ;
  CGFloat width = imageSize .width ;
  CGFloat height = imageSize .height ;
     
  if (width <= newSize .width && height <= newSize .height ){
   return image;
  }
     
  if (width == 0 || height == 0 ){
   return image;
  }
     
  CGFloat widthFactor = newSize .width / width;
  CGFloat heightFactor = newSize .height / height;
  CGFloat scaleFactor = (widthFactor<heightFactor?widthFactor:heightFactor);
     
  CGFloat scaledWidth = width * scaleFactor;
  CGFloat scaledHeight = height * scaleFactor;
  CGSize targetSize = CGSizeMake(scaledWidth,scaledHeight);
     
     UIGraphicsBeginImageContext(targetSize);
     [image drawInRect :CGRectMake( 0 , 0 ,scaledWidth,scaledHeight)];
     UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();
     return newImage;
}

你可能感兴趣的:(图像压缩)