gpt4 book ai didi

ios - 使用 AFNetworking 在异步模式下调整 UIImage 的大小

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:04:01 27 4
gpt4 key购买 nike

我在我的应用程序中使用 AFNetwork 从我的网络服务中获取一些数据。在这些数据中,有一些随机大小的图像,我需要添加一些 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;

基本上这会以适当的比例设置图像,并切割所有边缘..

最佳答案

AF UIImageView 类别有一个变体,它在异步图像请求完成时调用一个 block 。

您可以在成功 block 中调整返回图像的大小并将其分配给 ImageView 。见

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

调整图像大小后,请确保使用 GCD 将图像分配给主队列上的 UIImageView,因为该 block 将在后台线程中调用。

这是一个使用 UIIImageView 并将其内容模式设置为 AspectFill 的示例。使用 Core Graphics 调整大小通常比让 UIImageView 重新缩放图像看起来更好。

__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
}];

关于ios - 使用 AFNetworking 在异步模式下调整 UIImage 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13136808/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com