Iphone 从url获取图像不清晰&;清楚的

Iphone 从url获取图像不清晰&;清楚的,iphone,objective-c,Iphone,Objective C,我正在尝试从appstore的链接加载应用程序截图。 我得到的图像,但不是那么尖锐和清晰。我在背景中获取图像&一切正常。但是结果图像看起来有点模糊。我正在视网膜显示器上测试这个图像。任何人都知道为什么会这样。任何解决方案都会有帮助 谢谢 以下是我的图像加载代码: I am working on one application where i have to load images from server. 我做错了什么。问题是您正在以自然大小显示图像。在视网膜设备上,你需要的图像宽度和高度都是

我正在尝试从appstore的链接加载应用程序截图。 我得到的图像,但不是那么尖锐和清晰。我在背景中获取图像&一切正常。但是结果图像看起来有点模糊。我正在视网膜显示器上测试这个图像。任何人都知道为什么会这样。任何解决方案都会有帮助

谢谢

以下是我的图像加载代码:

I am working on one application where i have to load images from server.

我做错了什么。

问题是您正在以自然大小显示图像。在视网膜设备上,你需要的图像宽度和高度都是它们所进入的视图的两倍

假设图像是200x200,您将在100x100视图中显示它。正确的方法是:

  • 获取数据的CGImageRef

  • 使用以下方法创建一个UIImage,比例为2(用于视网膜)

    • (UIImage*)带有CGImage的图像:(CGImageRef)imageRef比例:(CGFloat)比例方向:(UIImageOrientation)方向
结果是图像大小为100x100,但比例为2



也就是说,由于您指定了“UIViewContentModeScaleAspectFit”,您可能只需要获取200x200图像并将其转移到UIImageView,但在这种情况下,您必须强制imageView的frame.size为100x100。

如果图像大小与imageView不同,然后可以根据视图按比例缩放服务器映像

// This will create the imageview with required frame & use the url to load the image
-(void)loadAppsScreenShots:(int)i Frame:(CGRect)frame withImageUrl:(NSString *)urlStr
{
    UIImageView *appImageView = [[UIImageView alloc] init];
    frame.origin.x = 0;
    appImageView.frame = frame;
    appImageView.tag = i;
    sharedImageCache = [ImageCache sharedImageCacheInstance];
    UIImage *image1 = [sharedImageCache getCachedImage:[NSString stringWithFormat:@"%@",urlStr]];
    if (image1==nil) 
    {       
        // Show indicator till image loads     
        UIActivityIndicatorView *indiView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        indiView.center = CGPointMake(appImageView.frame.size.width/2, appImageView.frame.size.height/2);
        [appImageView addSubview:indiView];
        [indiView startAnimating];
        indiView.hidden = FALSE;

        // Show label indicating image loading process 
        UILabel *loadingLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 25)];
        loadingLbl.text = @"";//@"Please wait...";
        loadingLbl.center = CGPointMake(appImageView.frame.size.width/2 + 5, appImageView.frame.size.height/2 + 23);
        loadingLbl.font = [UIFont fontWithName:@"Helvetica-Bold" size:15.0f];
        loadingLbl.textAlignment = UITextAlignmentCenter;
        loadingLbl.backgroundColor = [UIColor clearColor];
        loadingLbl.textColor = [UIColor whiteColor];
        [appImageView addSubview:loadingLbl];
        [appImageView sendSubviewToBack:loadingLbl];

        loadingLbl.hidden = FALSE;
        // Dictionalry to get all objects & pass it to method where we load the data
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];

        [dict setObject:appImageView forKey:@"imageView"];
        if (urlStr != nil) {
            [dict setObject:urlStr forKey:@"url"];
        }
        [dict setObject:indiView forKey:@"indi"];
        [dict setObject:loadingLbl forKey:@"loadingLbl"];
        [self performSelectorInBackground:@selector(loadImageFromURLAndSaveInDocDir:) withObject:dict];
    }
    else 
    {
        appImageView.image = image1;
    }
    [[appView viewWithTag:i] addSubview:appImageView];
    [appView bringSubviewToFront:appImageView];
    appImageView.contentMode = UIViewContentModeScaleAspectFit;

    appImageView=nil;
}


-(void)loadImageFromURLAndSaveInDocDir:(NSMutableDictionary *)dict
{
    @autoreleasepool 
    {
        UIImageView *cellImageViewObj = [dict objectForKey:@"imageView"];
        NSString *url;
        UIActivityIndicatorView *indiview = [dict objectForKey:@"indi"];
        UILabel *Lbl = [dict objectForKey:@"loadingLbl"];        

        if ([dict objectForKey:@"url"]) 
        {
            url = [dict objectForKey:@"url"];

            // fetch the data
            NSURL *imgURL = [NSURL URLWithString:url];
            NSData *imgData = [NSData dataWithContentsOfURL:imgURL];
            NSString *filename = [Utils getFileNameFromURL:url];
            // Cache the image
            [sharedImageCache cacheImage:[NSString stringWithFormat:@"%@",filename] :imgData];
            UIImage *image1 = [[UIImage alloc] initWithData:imgData];
            cellImageViewObj.image = image1;
            image1=nil;
        }
        else {
            url = @"";
        }
        // set the content mode & hide the indicator & label
        cellImageViewObj.contentMode = UIViewContentModeScaleAspectFit;
        [indiview stopAnimating];
        indiview.hidden = TRUE;
        Lbl.hidden = TRUE;
        dict = nil;
    }
}

除了显示图像外,您是否对图像执行其他操作?无论如何,你能给我们一个有这个问题的图像的源URL,以及你希望它显示的大小吗?你正在显示的图像和imageview的大小是多少。不,我没有对图像执行任何操作。我只是照原样显示图像。但这是造成我图像模糊的实际情况。在视网膜上,图像显示模糊,因为它的分辨率为320x480。所以我根据视网膜放大了它的工作原理。无论如何谢谢你的帮助谢谢大卫。通过这种方式,图像现在看起来很清晰。基本上,图像都是320x480的&我现在显示的是视网膜显示设备中的图像。这看起来像是模糊的。如果原始图像是320x480,并且你在视网膜设备上显示它,它将看起来模糊,因为它是设备分辨率的一半。你需要一张640x960的图像才能在iphone上获得完整的分辨率。我遗漏了什么吗?是的,那可能是更好的选择。感谢Ankit如果解决方案正常工作,请对评论进行投票并接受答案。
- (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize;

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