Load url from array to ImageView in Xcode - objective-c

I need load url from array (arrayCoverURL) to ImageView.
NSURL *imageURL = [NSURL URLWithString:[arrayCoverURL objectAtIndex:indexPath.item]];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:imageURL];
cell.imageView.image = [UIImage imageWithData:imageData];

Related

iOS decodeBase64ToImage returning nil on valid base 64 encoded image

I am trying to turn a base 64 canvas string into a UIImage but I always get nil.
Here is strEncodeData as a gist
https://gist.github.com/blasto333/5f15ab56dee0dbf790d90e9064160ea7#file-base64-receipt-image
Code
- (UIImage *)decodeBase64ToImage:(NSString *)strEncodeData
{
NSData *data = [[NSData alloc]initWithBase64EncodedString:strEncodeData options:NSDataBase64DecodingIgnoreUnknownCharacters];
return [UIImage imageWithData:data];
}
data is ALWAYS nil
The base64 string is not a normal base64 encoded string. It's a special data: schemed URL. It begins with data:image/png;base64, which is then followed by the encoded data.
You need:
- (UIImage *)decodeBase64ToImage:(NSString *)strEncodeData {
NSURL *url = [NSURL URLWithString: strEncodeData];
NSData *imageData = [NSData dataWithContentsOfURL: url];
UIImage *image = [UIImage imageWithData: imageData];
return image;
}

Unable to fetch data from nsurl

NSURL *url = [NSURL URLWithString:[self.imageArray objectAtIndex:indexPath.row]];
NSLog(#"My URL : %#",url);
NSData *data = [NSData dataWithContentsOfURL:url];
NSLog(#"New Data : %#",data);
UIImage *tmpImage = [[UIImage alloc] initWithData:data];
cell.imageView.image = tmpImage;
i think i got your problem, check this like my upload image in your project.
i also make a gif about how to add this setting in your project , just follow the step in my gif.

Adjusting UIImageView size and location based on the size of image fetched from service

In my iOS7 app I have view just like profile page in any social networking site. I have a profile image in top left corner. I am fetching the image from service url and loading it to a UIImageview.
My problem is, I have to increase the size of this image view when the size of the image from service is big. And also i have to adjust other UILabels beside this image view. I am using this code to get the size of image from url:
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data cache:NO];
CGSize size = img.size;
I don't know initWithData:cache: method on UIImage class, is this category thing?
Anyway you can make UIImageView dimension to fit content UIImage by [imageView sizeToFit].
If you want to adjust scale, use [UIImage imageWithData:data scale:scale].
NSString *path = #"http://www.example.com/example#x2.png"
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data scale:2.0];
_imageView.image = img;
[_imageView sizeToFit];

How to show next image on image view from image Array by clicking on button

I have image Array with two images out of that first image its showing properly on image View, for that i write code in view Did Load as`
NSString *urlString=[NSString stringWithFormat:#"%#",[imageArray1 objectAtIndex:i]];
NSLog(#"selected image:%#",urlString);
NSURL *url =[NSURL URLWithString:urlString];
NSData *imagedata =[[NSData alloc]initWithContentsOfURL:url];
UIImage *image=[[UIImage alloc]initWithData:imagedata];
[imgvw setImage:image];
Now i need to show next image on clicking bar button for that code is `
i=i+1;
NSLog(#"i=%d",i);
NSString *urlstr=[NSString stringWithFormat:#"%#",[imageArray1 objectAtIndex:i]];
NSLog(#"selected image:%#",urlstr);
NSURL *url =[NSURL URLWithString:urlstr];
NSData *imagedata =[[NSData alloc]initWithContentsOfURL:url];
UIImage *image1=[[UIImage alloc]initWithData:imagedata];
[imgvw setImage:image1];
but crashes at line `NSString *urlstr=[NSString stringWithFormat:#"%#",[imageArray1 objectAtIndex:i]];
I didn't understand why it happens please suggest something?
int i=0;
NSString *urlString=[NSString stringWithFormat:#"%#",[imageArray1 objectAtIndex:i]];
SURL *url =[NSURL URLWithString:urlString];
NSData *imagedata =[[NSData alloc]initWithContentsOfURL:url];
UIImage *image=[[UIImage alloc]initWithData:imagedata];
[imgvw setImage:image];
in button action event.
if(i<[imageArray1 count]-1)
{
i++;
NSString *urlString=[NSString stringWithFormat:#"%#",[imageArray1 objectAtIndex:i]];
SURL *url =[NSURL URLWithString:urlString];
NSData *imagedata =[[NSData alloc]initWithContentsOfURL:url];
UIImage *image=[[UIImage alloc]initWithData:imagedata];
[imgvw setImage:image];
}
else
{
//
/NSLog(#" you reach the last items ");
}
Declare int i; in yourviewcontroller.h file
i = 0; in viewDidLoad method
You implement code when you pressed button.
int noOfImages = [imageArray1 count];
if(++i< noofImages){
NSString *urlString=[NSString stringWithFormat:#"%#",[imageArray1 objectAtIndex:i]];
SURL *url =[NSURL URLWithString:urlString];
NSData *imagedata =[[NSData alloc]initWithContentsOfURL:url];
UIImage *image=[[UIImage alloc]initWithData:imagedata];
[imgvw setImage:image];
}
else
{
i =0;
NSString *urlString=[NSString stringWithFormat:#"%#",[imageArray1 objectAtIndex:i]];
SURL *url =[NSURL URLWithString:urlString];
NSData *imagedata =[[NSData alloc]initWithContentsOfURL:url];
UIImage *image=[[UIImage alloc]initWithData:imagedata];
[imgvw setImage:image];
}

Improve speed - UIImage from NSURL

I'm using the following code to fetch some images, that are going into a tableview. But it takes ages (5-6 seconds) to get the 30 images in.
Is there a smarter/faster way to do this?
NSString *imageUrl = ......;
NSString *urlStr =
[imageUrl UrlstringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *path = [[NSString alloc] initWithFormat:#"http://xxx.dk/xml.aspx%#", urlStr];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
return img;
[..... release];
Here profileimg is an get from Api ,String name
NSString *profileimg=[Check objectForKey:#"IMAGE"];
imageview.image=[[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:profileimg]]];
I'd rather download every new image in array of images when a cell appears on the screen and just display already saved image if a cell appears on the screen for the second time (i.e. image for this cell has been already downloaded).
use asynchronize loading of image.
check out this sample code : http://developer.apple.com/library/ios/#samplecode/LazyTableImages/Introduction/Intro.html