gpt4 book ai didi

iphone - 使用 MKNetworkKit setImageFromURL 时如何获取图像帧?

转载 作者:行者123 更新时间:2023-11-28 22:33:42 24 4
gpt4 key购买 nike

我正在尝试使用 setImageFromURL 下载图像后获取图像的大小:

UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[UIImageView setDefaultEngine:appDelegate.imageCache];
[img setImageFromURL:[NSURL URLWithString:@"http://testurl.com/testimg.jpg"]];
//img.frame = CGRectMake (0, 0, img.image.size.width, img.image.size.height);
[self.view addSubview:img];

我正在使用 MKNetworkKit——有没有办法将完成处理程序添加到 setImageFromURL 方法?

最佳答案

您可以应用自己的完成 block

评论不多,这种情况请使用Grand Central Dispatch来提高代码效率和多核利用率。

示例:

- (void) setImageFromUrl:(NSString*)urlString {  

[self setImageFromUrl:urlString completion:NULL];
}

- (void) setImageFromUrl:(NSString*)urlString
completion:(void (^)(void))completion {

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

NSLog(@"Starting: %@", urlString);
UIImage *avatarImage = nil;
NSURL *url = [NSURL URLWithString:urlString];
NSData *responseData = [NSData dataWithContentsOfURL:url];
avatarImage = [UIImage imageWithData:responseData];
NSLog(@"Finishing: %@", urlString);

if (avatarImage) {
dispatch_async(dispatch_get_main_queue(), ^{
self.image = avatarImage;
//Assign the image to ur ImageView.
//get the image frame from ur ImageView.frame
});
dispatch_async(dispatch_get_main_queue(), completion);
}
else {
NSLog(@"-- impossible download: %@", urlString);
}
});
}

更多详情,请访问 iOS GCD magic

关于iphone - 使用 MKNetworkKit setImageFromURL 时如何获取图像帧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16705360/

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