+(UIImage *)scaleImage:(UIImage *)_image size:(CGSize)_size
{
CGFloat _toWidth = _size.width;
CGFloat _toHeight = _size.height;
float _x = 0.0f;
float _y = 0.0f;
CGRect _frame = CGRectMake(_x, _y, _toWidth, _toHeight);
float _oldWidth = _image.size.width;
float _oldHeight = _image.size.height;
float _scaleRatio = MAX( (_toWidth / _oldWidth), (_toHeight / _oldHeight) );
float _equalWidth = (int)( _oldWidth * _scaleRatio );
float _equalHeight = (int)( _oldHeight * _scaleRatio );
UIGraphicsBeginImageContext( CGSizeMake(_equalWidth, _equalHeight) );
[_image drawInRect:CGRectMake(0, 0, _equalWidth, _equalHeight)];
_image = nil;
_image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
_x = floor( (_equalWidth - _toWidth) / 2 );
_y = floor( (_equalHeight - _toHeight) / 2 );
_frame = CGRectMake(_x, _y, _toWidth, _toHeight);
CGImageRef _smallImage = CGImageCreateWithImageInRect( [_image CGImage], _frame );
UIImage *_doneImage = [UIImage imageWithCGImage:_smallImage];
CGImageRelease(_smallImage);
return _doneImage;
}
沒有留言:
張貼留言