简体   繁体   English

如何在没有执行NSData转换的情况下将URL图像传递给ios中的按钮

[英]How to pass an url image to button in ios with out doing NSData conversion

I wants to display a imageurl on button from the server.In the server i have image url's. 我想在服务器上显示按钮上的图像。在服务器中我有图像网址。 I wants to do user interactions so i am use a button. 我想做用户交互,所以我使用一个按钮。

I have done below: 我在下面做了:

  1. Retrieve the image url from server. 从服务器检索图像URL。
  2. Convert > NSData to ImageObject 将> NSData转换为ImageObject
  3. Added the imageObject to setImageProperty then it's sucessfully displaying the image but the performance was slowly. 将imageObject添加到setImageProperty然后它成功显示图像,但性能很慢。

but I don't want to use NSData operations. 但我不想使用NSData操作。 Because it's take so much time.I wants to show the good performance in the app. 因为这需要花费很多时间。我想在应用程序中展示出良好的性能。

Is there any way to solve this problem? 有什么方法可以解决这个问题吗?

You can use SDWebImage library. 您可以使用SDWebImage库。 its good library with caching functionality. 它具有缓存功能的好库。

Just integrate below code. 只需集成下面的代码。

add below code in appDidFinishLauching because using lifo execution last image will be download first appDidFinishLauching添加以下代码,因为使用appDidFinishLauching执行最后一个图像将首先下载

SDWebImageManager.sharedManager.imageDownloader.executionOrder = SDWebImageDownloaderLIFOExecutionOrder;

then import class in your viewcontroller. 然后在viewcontroller中导入类。

#import "UIButton+WebCache.h"

and for integration 并用于整合

//set button image
[btn sd_setImageWithURL:[NSURL URLWithString:@"your string"] forState:UIControlStateNormal];

//set background image
[btn sd_setBackgroundImageWithURL:[NSURL URLWithString:@"your string"] forState:UIControlStateNormal];

you can also download image in background thread using below code 你也可以使用下面的代码在后台线程下载图像

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"your string with encoding"];
    if (data)
    {
        UIImage *img = [UIImage imageWithData:data];
        dispatch_async(dispatch_get_main_queue(), ^{
            if (img)
               [btn setImage:btn forState:UIControlStateNormal];
        });
    }
});

But I presonally prefer SDWebImage library because it is good library and it will handle cache so you dont want to cache image. 但我原本更喜欢SDWebImage库,因为它是一个很好的库,它会处理缓存,所以你不想缓存图像。

Another easy solution would be to use AFNetworking 's UIButton category to set the image. 另一个简单的解决方案是使用AFNetworkingUIButton类别来设置图像。

-(void)setImageForState:withURL:

This method will handle all the nitty gritty of downloading and converting the image. 这种方法将处理下载和转换图像的所有细节。 Your UI will not block because it is an async process. 您的UI不会阻止,因为它是异步进程。 This will give your users a better overall experience. 这将为您的用户提供更好的整体体验。

You can use dispatch queue.Here also converting NSString image URL to NSDATA, but it will work fast. 您可以使用调度队列。也可以将NSString图像URL转换为NSDATA,但它可以快速工作。

dispatch_queue_t myQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
        dispatch_async(myQueue, ^{
            NSString *ImageURL = @"yourURL.jpg";
            NSData *imageData;
            if (ImageURL == nil || ImageURL == (id)[NSNull null] || [[NSString stringWithFormat:@"%@",ImageURL] length] == 0 || [[[NSString stringWithFormat:@"%@",ImageURL] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0)
            {
            }

            else
            {
                imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]];

            }
            dispatch_async(dispatch_get_main_queue(), ^{

                if (ImageURL == nil || ImageURL == (id)[NSNull null] || [[NSString stringWithFormat:@"%@",ImageURL] length] == 0 || [[[NSString stringWithFormat:@"%@",ImageURL] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0)
                {
                    imageView.image=[UIImage imageNamed:@"photo_frame_noimage.png"];
                }
                else if (imageData == nil || imageData == (id)[NSNull null] || [[NSString stringWithFormat:@"%@",imageData] length] == 0 || [[[NSString stringWithFormat:@"%@",imageData] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0)
                {
                    imageView.image=[UIImage imageNamed:@"photo_frame_noimage.png"];
                }
                else
                {
                    imageView.image=[UIImage imageWithData:imageData];
                }
            });

        });

I do not know whether it will resolve your loading time or not, but this will surely reduce application running cycle. 我不知道它是否会解决你的加载时间,但这肯定会减少应用程序的运行周期。

[btn setImage:[UIImage imageWithCIImage:[CIImage imageWithContentsOfURL:theURL]] forState:UIControlStateNormal];

Similar Question: Set UIImageView image using a url 类似问题: 使用url设置UIImageView图像

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM