簡體   English   中英

使用SDWebImage為imageview加載圖像,如何獲得圖像的寬度?

[英]Use SDWebImage to load a image for imageview, How can i get the width of image?

當我使用setImageWithURL加載在線圖像時,我得到了0.000000,但是當我加載本地圖像時,我得到了正確的寬度。

NSURL * url1=[NSURL URLWithString:@"http://img.hb.aicdn.com/7f46b5eb65320f18f797a4a26ebe9ba328b547832f7b3-KYR0Xn_fw192"];
UIImageView * img2=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 192,400)];
[img2 setImageWithURL:url1];
//[img2 setImage:[UIImage imageNamed:@"0.gif"]];
//load local image

[self.view addSubview:img2];
CGFloat height1=img2.image.size.width;
NSLog(@"%f",height1);

我知道這是一個非常老的問題,但不久前我遇到了同樣的問題,並認為我的回答可以幫助遇到相同問題的人。

要獲取圖像寬度,您應該使用SDWebImageManager獨立下載圖像。 我很確定在他們的Wiki頁面中對此進行了解釋。

這是執行任務的示例代碼:

-(UIImage *)downloadImageAsync:(NSString *)imageKey
{
    NSLog(@"image key : %@",imageKey);
    __block UIImage *returnedImage;
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    [manager downloadWithURL:[NSURL URLWithString:imageKey] options:0 progress:^(NSUInteger receivedSize, long long expectedSize){
        NSLog(@"received size %d expected size %lld", receivedSize, expectedSize);

    }completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType,BOOL finished){
        if(image && finished) {
            returnedImage = image;
        }
    }];

    if(returnedImage == nil) {
        NSLog(@"Returned Image Nil");
    }
    return returnedImage;
}

因此,當UIImage完成下載時,您可以獲得其寬度和高度。 希望這可以幫助。

干杯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM