Jyoshna
Jyoshna

Reputation: 253

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.
  2. Convert > NSData to ImageObject
  3. Added the imageObject to setImageProperty then it's sucessfully displaying the image but the performance was slowly.

but I don't want to use NSData operations. 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?

Upvotes: 4

Views: 2927

Answers (4)

Andrew Monshizadeh
Andrew Monshizadeh

Reputation: 1784

Another easy solution would be to use AFNetworking's UIButton category to set the image.

-(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. This will give your users a better overall experience.

Upvotes: 3

Mrunal
Mrunal

Reputation: 14118

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

Hope this helps.

Upvotes: 0

ChintaN -Maddy- Ramani
ChintaN -Maddy- Ramani

Reputation: 5164

You can use SDWebImage library. 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

SDWebImageManager.sharedManager.imageDownloader.executionOrder = SDWebImageDownloaderLIFOExecutionOrder;

then import class in your 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.

Upvotes: 4

Nikita Khandelwal
Nikita Khandelwal

Reputation: 1741

You can use dispatch queue.Here also converting NSString image URL to NSDATA, but it will work fast.

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

        });

Upvotes: 0

Related Questions