来自网址的图片以及如何减少iOS中的图片大小

时间:2014-05-14 04:13:45

标签: ios image

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

1)。点击之前。

enter image description here

2)。单击

enter image description here

这是在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; 
  }); });

3 个答案:

答案 0 :(得分:0)

  1. 因为图像成了大问题。检查你的uiimage的内容模式看看它是否是你想要的,有一个枚举它。你可以搜索 文档中的UIViewContentMode了解更多详情。

  2. 调整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;
    }
    

答案 1 :(得分:0)

[newsbject.allnewsimg setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[newsarry objectAtIndex:indexPath.row] objectForKey:@"image_file"]]]

               placeholderImage:[UIImage imageNamed:@"songs85-56.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:@""]]];

                NSData *data = [NSData dataWithContentsOfURL:url];
                UIImage *image = [UIImage imageWithData:data];

                dispatch_async(dispatch_get_main_queue(), ^{

            [tableView cellForRowAtIndexPath:indexPath].imageView.image = image;
        });
    });

答案 2 :(得分:0)

检查此代码。

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