2 ooomiao oOOMIAO 于 2013.06.27 11:42 提问

调整UIImage尺寸适应UIImageView

有一个 3264 × 2448图片, imageView尺寸768 × 1024,我需要调整图片尺寸适应视图,但是不能让图片质量下降。

试过下面的方法:

- (UIImage *)imageByScalingProportionallyToSize: (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;
        else
            scaleFactor = heightFactor;
        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;
       }
    }
    // this is actually the interesting part:
    UIGraphicsBeginImageContext(targetSize);
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width  = scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    [sourceImage drawInRect:thumbnailRect];
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    if(newImage == nil) NSLog(@"could not scale image");
    return newImage ;
}

1个回答

Chengzi_963
Chengzi_963   2013.06.27 15:03
已采纳

试试

+ (UIImage*)imageWithImage:(UIImage*)image 
               scaledToSize:(CGSize)newSize;
{
   UIGraphicsBeginImageContext( newSize );
   [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
   UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();

   return newImage;
}

指定新尺寸(768,1024) 然后传递初始图片,但是要保证高宽比和原始的比例一样。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐
让UIImageView适应图片的大小
我在一个UIImageView中加入了一张图片,我想让这个图片的尺寸作为该UIImageView的尺寸。 //创建imgV视图 -(void)createImgV { //创建视图展示 _imgV = [[UIImageView alloc] initWithFrame:CGRectZero]; _imgV.backgroundColor=[UIColor ye
iOS UIImage根据屏宽调整size
需求:UIImage根据屏幕宽度按照自己本身比例改变高度 上代码,为UIImage创建一个Category #import &quot;UIImage+UIImageExtras.h&quot; @implementation UIImage (UIImageExtras) - (UIImage *)imageByScalingToSize:(CGSize)targetSize { UIImag
UIImageView中图片尺寸模式
UIViewContentModeScaleToFill, UIViewContentModeScaleAspectFit, // contents scaled to fit with fixed aspect. remainder is transparent UIViewContentModeScaleAspectFill, // contents scaled to fill with fixed aspect. some portion of content ma
iOSUIImageView处理图片大小问题
iOSUIImageView处理图片大小问题第一种:UIImageView *myImageView = [[ UIImageView alloc] initWithImage: [UIImage imageNamed: @”demo”]]; 用该方法可以显示图片原有大小。 第二种方法: UIImageView *myImageView = [[UIImage alloc] initWithFr
uiimageview 读取uiimage的大小
UIImage* image = [UIImage imageNamed:@"image.png"];    UIImageView* imageView = [[[UIImageView alloc] initWithImage:image] autorelease];    imageView.frame = CGRectMake(0, 0, 300, 200);    imageVie
根据网络图片的size大小设置UIImageView的大小
有时候在设置UIImageView的大小时候需要根据UIimage的长宽比来自动设置,不让图片原比例失真。 如果是从本地获取到的图片,[UIImage imageNamed:@""]; 这样就可以拿到image了,从而获取到image的size。但大多数时候我们都是网络请求拿到的图片, 我们需要 NSData *data = [NSData dataWithContentsOfURL
Swift - 改变UIImage图片的大小尺寸,或按比例缩放
在开发中,我们有时候需要对原始的 UIImage 进行处理,比如修改大小或者进行缩放操作。           1,扩展UIImage 这里先对 UIImage 进行扩展,增加两个方法,分别用于尺寸的重置和大小缩放。 1 2 3 4 5 6 7 8 9 10 11 12
iOS UIImageView自适应图片大小
窗口大小获取:     CGRect screenBounds = [ [UIScreenmainScreen]bounds];//返回的是带有状态栏的Rect CGRect rect = [ [UIScreenmainScreen]applicationFrame];//不包含状态栏的Rect UIImageView: 一 :圆角以及自适应图片大小    UIIma
ios UIImageView处理图片大小问题
UIImageView视图可以显示图片 实例化UIImageView有两种方法 第一种方法: UIImageView *myImageView = [[ UIImageView alloc] initWithImage: [UIImage imageNamed: @”demo”]]; 用该方法可以显示图片原有大小。第二种方法: UIImageView *myImageView = [[UIImag...
图片自适应uiimageview
折腾了半天 原来图片有自适应的这种属性  晕! [_baseImageView setClipsToBounds:YES];         [_baseImageView setContentMode:UIViewContentModeScaleAspectFit];        _baseImageView.frame = [UIScreen mainScreen].bo