How to pass url to button in ios without NSData conversion

I want to show the imageurl button on a button from the server. On the server, I have an image url. I want user interactions, so I use a button.

I have done the following:

  • Get the URL of the image from the server.
  • Convert> NSData to ImageObject
  • The imageObject is added to setImageProperty, after which it successfully displays the image, but the performance was slow.

but I do not want to use operations NSData. Because it takes a lot of time. I want to show good performance in the application.

Is there any way to solve this problem?

+4
source share
4

AFNetworking UIButton .

-(void)setImageForState:withURL:

. , . .

+3

SDWebImage . .

.

appDidFinishLauching, lifo .

SDWebImageManager.sharedManager.imageDownloader.executionOrder = SDWebImageDownloaderLIFOExecutionOrder;

view.

#import "UIButton+WebCache.h"

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

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

,

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

SDWebImage, , , .

+4

. URL- NSString 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];
                }
            });

        });
0

, , , , .

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

: UIImageView URL

, .

0

Source: https://habr.com/ru/post/1568319/


All Articles