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

アプリストアのリンクからアプリケーションのスクリーンショットを読み込もうとしています。私は画像を取得していますが、それほど鮮明ではありません。バックグラウンドで画像を取得していますが、すべて正常に動作しますが、結果の画像は少しぼやけて見えます。この画像を Retina ディスプレイでテストしています。なぜそれが起こっているのか、誰もが何らかの考えを持っています。どんな解決策も役に立ちます。

ありがとう、

画像をロードするための私のコードは次のとおりです。

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

何が間違っているのですか。

4

2 に答える 2

0

問題は、画像を自然なサイズで表示していることです。Retina デバイスでは、描画されるビューの 2 倍の幅と 2 倍の高さの画像が必要です。

画像が 200x200 で、100x100 のビューで表示するとします。これを行う適切な方法は次のとおりです。

  • データの CGImageRef を取得する

  • 以下のメソッドとスケール 2 (網膜用) を使用して UIImage を作成します。

    • (UIImage *)imageWithCGImage:(CGImageRef)imageRef スケール:(CGFloat)スケール方向:(UIImageOrientation)方向

結果は、サイズが 100x100 の画像ですが、スケールは 2 です。


つまり、「UIViewContentModeScaleAspectFit」を指定しているので、200x200 の画像を取り出して UIImageView に渡すことができるかもしれませんが、この場合は imageView の frame.size を 100x100 にする必要があります。

于 2012-08-21T11:27:40.983 に答える
0

画像サイズが imageview と異なる場合は、ビューに従ってサーバー イメージを比例的にスケーリングできます。

- (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 ;
}
于 2012-08-21T11:37:07.527 に答える