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

Ios 从url获取图像以及如何在iPhone中减小图像大小,ios,objective-c,image,Ios,Objective C,Image,我的问题是:我正在从服务器获取映像。当我从服务器获取图像时,图像大小为138x91,但我给出的图像大小为我的自定义单元格大小85x56。装载良好。但当我单击行时,它会增加服务器映像的大小。如何在tableview中获取图像时减小图像大小,以及如何在单击“行”时不更改图像大小。如何解决我的问题。我希望从url获取图像,以及如何在iOS.goto bellow链接中减小图像大小。 1) 。然后单击。 2) 。经常点击 这是在显示一个占位符图像之前,获取索引路径行单元格中的数据代码并加载的图像

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

2) 。经常点击

这是在显示一个占位符图像之前,获取索引路径行单元格中的数据代码并加载的图像

 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 *)resizeImage:(UIImage *)image withWidth:(int) width withHeight:(int) height {

    CGImageRef imageRef = [image CGImage];
    CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
    CGColorSpaceRef colorSpaceInfo = CGColorSpaceCreateDeviceRGB();

    if (alphaInfo == kCGImageAlphaNone)
        alphaInfo = kCGImageAlphaNoneSkipLast;

    CGContextRef bitmap;

    if (image.imageOrientation == UIImageOrientationUp | image.imageOrientation == UIImageOrientationDown) {
        bitmap = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, alphaInfo);

    } else {
        bitmap = CGBitmapContextCreate(NULL, height, width, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, alphaInfo);

    }

    if (image.imageOrientation == UIImageOrientationLeft) {
        NSLog(@"image orientation left");
        CGContextRotateCTM (bitmap, DEGREES_TO_RADIANS(90));
        CGContextTranslateCTM (bitmap, 0, -height);

    } else if (image.imageOrientation == UIImageOrientationRight) {
        NSLog(@"image orientation right");
        CGContextRotateCTM (bitmap, DEGREES_TO_RADIANS(-90));
        CGContextTranslateCTM (bitmap, -width, 0);

    } else if (image.imageOrientation == UIImageOrientationUp) {
        NSLog(@"image orientation up");

    } else if (image.imageOrientation == UIImageOrientationDown) {
        NSLog(@"image orientation down");
        CGContextTranslateCTM (bitmap, width,height);
        CGContextRotateCTM (bitmap, DEGREES_TO_RADIANS(-180.));

    }

    CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);
    CGImageRef ref = CGBitmapContextCreateImage(bitmap);
    UIImage *result = [UIImage imageWithCGImage:ref];

    CGContextRelease(bitmap);
    CGImageRelease(ref);

    return result;
}
尝试以下方法:

 UIImage *croppedimg = [self imageWithImage:[UIImage imageNamed:@"image.png"] convertToSize:CGSizeMake(95, 95)]; // here resize your image whatever you want.

 - (UIImage *)imageWithImage:(UIImage *)image convertToSize:(CGSize)size
 {
    UIGraphicsBeginImageContext(size);
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return destImage;
 }

如果不在tableview代理中查看代码,很难确定问题到底是什么

然而,我认为,如果您在imageView中设置适当的
contentMode
,将解决您的问题

[newsobject.allnewsimg setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[newsarry objectAtIndex:indexPath.row] objectForKey:@"image_file"]]]
               placeholderImage:[UIImage imageNamed:@"songs85-56.png"]];
设置图像后,相应地设置contentMode,可能类似于:

newsobject.imageView.image = [UIImage imageNamed:@"Placeholder.png"];
newsobject.imageView.contentMode = UIViewContentModeScaleToFill;

使用
UIImageView+WebCache.h和.m
类下载图像。您可以在github或google上搜索这些类。它为您提供了不同的异步下载图像的方法,还提供了根据我们的应用程序要求调整图像大小的功能。使用以下方法:

- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options: (SDWebImageOptions)options  andResize:(CGSize)size withContentMode:(UIViewContentMode)mode;

我希望这可以帮助您解决您的问题。

您能显示
CellForRowatineXpath
的完整代码吗?我正在下载并导入这一个UIImageView+WebCache.h,并实现此方法bun没有解决我的问题,并且在运行时未调用此方法program@venky如果该方法未获得调用,然后,首先您应该调试程序,了解为什么该行无法执行。当我实现此方法时,出现了两个错误(1)。“_度到_弧度”,引用自:2)。叮当:错误:链接器命令失败,退出代码为1(使用-v查看调用)#定义度到_弧度(角度)((角度)/180.0*mpi)使用此命令时,我很容易解决图像大小(从小到大)问题。我们必须下载'SDWebImage'框架并导入#导入“UIImageView+WebCache.h”这个。
[newsobject.allnewsimg setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[newsarry objectAtIndex:indexPath.row] objectForKey:@"image_file"]]]
               placeholderImage:[UIImage imageNamed:@"songs85-56.png"]];