从url获取图像以及如何在iOS中减小图像大小

从url获取图像以及如何在iOS中减小图像大小,ios,image,Ios,Image,我的问题是:我正在从服务器获取映像。当我从服务器获取图像时,图像大小为138x91,但我给出的图像大小为我的自定义单元格大小85x56。装载良好。但当我单击行时,它会增加服务器映像的大小。如何在我的tableview中获取图像时减小图像大小,以及如何在单击行时不更改图像大小。如何解决我的问题。我希望从url获取图像,以及如何在iOS.goto bellow链接中减小图像大小 1.单击之前 2.经常点击 这是在cellForRowAtIndexPath中获取数据代码并在显示之前加载的图像 plac

我的问题是:我正在从服务器获取映像。当我从服务器获取图像时,图像大小为138x91,但我给出的图像大小为我的自定义单元格大小85x56。装载良好。但当我单击行时,它会增加服务器映像的大小。如何在我的tableview中获取图像时减小图像大小,以及如何在单击行时不更改图像大小。如何解决我的问题。我希望从url获取图像,以及如何在iOS.goto bellow链接中减小图像大小

1.单击之前

2.经常点击

这是在cellForRowAtIndexPath中获取数据代码并在显示之前加载的图像

placeholderimage . newsobject.imageView.image = [UIImage imageNamed:@"Placeholder.png"];   
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); 
dispatch_async(queue, ^{ 
    NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[newsarry objectAtIndex:indexPath.row] objectForKey:@"thumb_file"]]]; 
   NSData *data = [NSData dataWithContentsOfURL:url]; 
   UIImage *image = [UIImage imageWithData:data];  
   dispatch_async(dispatch_get_main_queue(), ^{ [tableView cellForRowAtIndexPath:indexPath].imageView.image = image; 
  }); });
因为形象成了大问题。检查你的uiimage的contentMode,看看它是否是你想要的,它有一个枚举。你可以搜索 有关详细信息,请参阅文档中的UIViewContentMode

要调整uiimage的大小,可以在完成后使用一些调整大小功能 从服务器获取图像,然后保存或显示。一个例子 调整大小功能的定义:

 - (UIImage*)resizedImage:(CGSize)bounds upScale:(BOOL)upScale {
    CGSize originalSize = self.size;
    float xScale = bounds.width / originalSize.width;
    float yScale = bounds.height / originalSize.height;
    float scale = MIN(xScale, yScale);
    if (!upScale) {
      scale = MIN(scale, 1);
    }

    CGSize newSize = CGSizeMake(originalSize.width * scale, originalSize.height * scale);
    UIGraphicsBeginImageContextWithOptions(newSize, NO, [UIScreen mainScreen].scale);
    [self drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return resultImage;
}

用这个代码检查

float actualHeight = image.size.height;
float actualWidth = image.size.width;
float imgRatio = actualWidth/actualHeight;
float maxRatio = 320.0/480.0;

if(imgRatio!=maxRatio){
    if(imgRatio < maxRatio){
        imgRatio = 480.0 / actualHeight;
        actualWidth = imgRatio * actualWidth;
        actualHeight = 480.0;
    }
    else{
        imgRatio = 320.0 / actualWidth;
        actualHeight = imgRatio * actualHeight;
        actualWidth = 320.0;
    }
}
CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
UIGraphicsBeginImageContext(rect.size);
[image drawInRect:rect];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

给我代码,你把图像插入一行吗?也许当你们点击一行时,这张图片会自动重设这张图片会在单元格中为索引路径的行获取数据代码,并在我显示一张占位符图片之前加载。newsobject.imageView.image=[UIImage ImageName:@Placeholder.png];调度队列=调度获取全局队列调度队列优先级高,0ul;dispatch_asyncqueue,^{NSURL*url=[NSURL URLWithString:[NSString stringWithFormat:@%@,[[newsarry objectAtIndex:indexath.row]objectForKey:@thumb\u file]];NSData*数据=[NSData data data WITHCONTENTSOFURL:url];UIImage*图像=[UIImage IMAGEWITH data:data];dispatch_asyncdispatch_get_main_队列,^{[tableView cellForRowAtIndexPath:indexPath].imageView.image=image;};};你应该添加有问题的代码,这样每个人都可以看到。当你得到图像时,请调整它的大小。当我使用此代码时,我可以解决我的图像延迟加载和滚动问题。
float actualHeight = image.size.height;
float actualWidth = image.size.width;
float imgRatio = actualWidth/actualHeight;
float maxRatio = 320.0/480.0;

if(imgRatio!=maxRatio){
    if(imgRatio < maxRatio){
        imgRatio = 480.0 / actualHeight;
        actualWidth = imgRatio * actualWidth;
        actualHeight = 480.0;
    }
    else{
        imgRatio = 320.0 / actualWidth;
        actualHeight = imgRatio * actualHeight;
        actualWidth = 320.0;
    }
}
CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
UIGraphicsBeginImageContext(rect.size);
[image drawInRect:rect];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();