2

アプリケーションでAFNetworkを使用して、Webサービスからデータを取得しています。これらのデータには、ランダムなサイズの画像がいくつかあります。UIImageViewを追加する必要があります。

私の問題は、非同期モードでUIImageViewの正しいスケールで画像を調整することです。

これを行う方法はありますか?

編集

私はAfnetwokingのプロジェクトの通常の方法を使用しています。

 [self setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"img_my_nophoto"]];

このメソッド内のロジックは次のとおりです。

- (void)setImageWithURLRequest:(NSURLRequest *)urlRequest 
          placeholderImage:(UIImage *)placeholderImage 
                   success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success
                   failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure
{
    [self cancelImageRequestOperation];

UIImage *cachedImage = [[[self class] af_sharedImageCache] cachedImageForRequest:urlRequest];
if (cachedImage) {
    self.image = cachedImage;
    self.af_imageRequestOperation = nil;

    if (success) {
        success(nil, nil, cachedImage);
    }
} else {
    self.image = placeholderImage;

    AFImageRequestOperation *requestOperation = [[AFImageRequestOperation alloc] initWithRequest:urlRequest];
    [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        if ([[urlRequest URL] isEqual:[[self.af_imageRequestOperation request] URL]]) {
            self.image = responseObject;
            self.af_imageRequestOperation = nil;
        }

        if (success) {
            success(operation.request, operation.response, responseObject);
        }

        [[[self class] af_sharedImageCache] cacheImage:responseObject forRequest:urlRequest];


    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if ([[urlRequest URL] isEqual:[[self.af_imageRequestOperation request] URL]]) {
            self.af_imageRequestOperation = nil;
        }

        if (failure) {
            failure(operation.request, operation.response, error);
        }

    }];

    self.af_imageRequestOperation = requestOperation;

        [[[self class] af_sharedImageRequestOperationQueue] addOperation:self.af_imageRequestOperation];
    }
}

完全なクラスコード: https ://github.com/AFNetworking/AFNetworking/blob/master/AFNetworking/UIImageView%2BAFNetworking.m

解決

UIImageViewの設定に関する問題を解決します

self.contentMode = UIViewContentModeScaleAspectFill;

self.clipsToBounds = YES;

基本的にこれは適切な縮尺で画像を設定し、すべてのエッジをカットします。

4

3 に答える 3

6

There is a variant of the AF UIImageView category that calls a block when the asynchronous image request is complete.

You can resize the returned image in the success block and assign it to the image view. See

- (void)setImageWithURLRequest:(NSURLRequest *)urlRequest 
              placeholderImage:(UIImage *)placeholderImage 
                       success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success
                       failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure

Once the image has been resized, make sure you assign the image to the UIImageView on the main queue using GCD as the block will be called in a background thread.

Here's an example using a UIIImageView with its content mode set to AspectFill. Resizing using Core Graphics will usually look better than having the UIImageView rescale the image.

__weak typeof(self) weakSelf = self;
CGSize targetSize = self.targetImageView.bounds.size;
[self.targetImageView setImageWithURL:photoURL success:^(UIImage *image) {

    CGFloat imageHeight = image.size.height;
    CGFloat imageWidth = image.size.width;

    CGSize newSize = targetSize;
    CGFloat scaleFactor = targetSize.width / imageWidth;
    newSize.height = imageHeight * scaleFactor;

    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *small = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    dispatch_async(dispatch_get_main_queue(),^ {

        weakSelf.targetImageView.image = small;
    });

} failure:^(NSError *error) {
    // Handel the error here
}];
于 2012-11-13T13:10:12.687 に答える
2

編集:

私があなたの問題を正しく理解していればimageView.contentMode = UIViewContentModeScaleAspectFit、Afnetworking FW から取得した ImageView を設定するだけで済みます。

または、次の方法で画像の実際のサイズを確認し、imageView.image.sizeそれに応じて imageView のフレームを設定することもできます。

于 2012-10-30T10:34:04.823 に答える
0

これがAFImageResponseSerializer目的です。取得する画像の縮尺に合わせて画像の縮尺を変更するだけです。

AFImageResponseSerializer *imageResponseSerializer = [self.avatarImageView imageResponseSerializer]; [imageResponseSerializer setImageScale:1.0];

于 2015-07-09T18:38:37.140 に答える