- (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; 
} 

发布评论

分享到:

IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

在iOS App的icon左上角显示圆形背景的数字详解
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。