I have an image link, now i want to load this image in my app and i using category
[test setImageWithURL:[NSURL URLWithString:#"close"]];
link image is:
close
i see that link image is quite ok, but imageview can not load the image. Please tell me why, thanks!
ok try this
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^(void) {
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://216.57.222.146:8080/Stafflink_Web/images/assets/5108029/5108029_0_resized.jpg"];
UIImage* image = [[UIImage alloc] initWithData:imageData];
if (image) {
dispatch_async(dispatch_get_main_queue(), ^{
if (cell.tag == indexPath.row) {
test.image = image;
}
});
}
});
Is test a UIImageView object? It needs to be.
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
[imageView setImageWithURL:[NSURL URLWithString:#"http://i.imgur.com/r4uwx.jpg"] placeholderImage:[UIImage imageNamed:#"placeholder-avatar"]];
Related
I am using the Given Code to download images from its respective url's, but due to some problem it's not working and i am unable to diagnose the problem. Please help me out this Problem.
Thanks in Advance.
-(void)createScrollViewControls
{
[self removeAllControlsFromScrollView];
totalNumberOfImages=[arrImages count];
__block int x=0;
__block int y=0;
__block int scroll=0;
__block UIImage *tempimag=nil;
if(totalNumberOfImages>0) //If product images are there
{
// imageScroll.contentSize=CGSizeMake(imageScroll.frame.size.width*totalNumberOfImages, IMAGEHEIGHT);
for(int i=0;i<totalNumberOfImages;i++){
NSLog(#"Value=%#",arrImages);
UIActivityIndicatorView *activity= [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(77+scroll, 38, 30, 30)];
[activity setBackgroundColor:[UIColor clearColor]];
[activity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
[imageScroll addSubview:activity];
[activity startAnimating];
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(concurrentQueue, ^{
NSURL *imageURL=[NSURL URLWithString:[arrImages objectAtIndex:i]];
NSData *tempData=[NSData dataWithContentsOfURL:imageURL];
UIImage *tImage=[UIImage imageWithData:tempData];
if (tImage==nil)
{
tImage=[UIImage imageNamed:#"NoImage.png"];
}
dispatch_async(dispatch_get_main_queue(), ^{
tempimag= tImage;
UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(x, y, IMAGEWIDTH, IMAGEHEIGHT)];
//if url is there but image not getting downloaded then show a static image
if (!tempimag)
{
imageScroll.contentSize=CGSizeMake(imageView.frame.size.width, IMAGEHEIGHT);
NSLog(#"no underlying data");
imageView.image = [UIImage imageNamed:kNoImageAvailable];
[imageScroll addSubview:imageView];
// totalNumberOfImages = 0;
}
else{
[activity stopAnimating];
imageScroll.contentSize=CGSizeMake(imageScroll.frame.size.width*totalNumberOfImages, IMAGEHEIGHT);
imageView.image=tempimag;
[imageScroll addSubview:imageView];
x=x+imageScroll.frame.size.width;
}
});
});
/*
UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(x, y, IMAGEWIDTH, IMAGEHEIGHT)];
if (!tempimag)//if url is there but image not getting downloaded then show a static image
{
imageScroll.contentSize=CGSizeMake(imageView.frame.size.width, IMAGEHEIGHT);
NSLog(#"no underlying data");
imageView.image = [UIImage imageNamed:kNoImageAvailable];
[imageScroll addSubview:imageView];
totalNumberOfImages = 0;
break;
}
else{
imageScroll.contentSize=CGSizeMake(imageScroll.frame.size.width*totalNumberOfImages, IMAGEHEIGHT);
imageView.image=tempimag;
[imageScroll addSubview:imageView];
x=x+imageScroll.frame.size.width;
}
} */
scroll+=184;
}
}
This below lines of code:
if (tImage==nil)
{
tImage=[UIImage imageNamed:#"NoImage.png"];
}
will always make your program control to go to:
if (!tempimag)
{
imageScroll.contentSize=CGSizeMake(imageView.frame.size.width, IMAGEHEIGHT);
NSLog(#"no underlying data");
imageView.image = [UIImage imageNamed:kNoImageAvailable];
[imageScroll addSubview:imageView];
// totalNumberOfImages = 0;
}
Please remove the first part of the code mentioned and try it out.
This is written in my viewDidLoad method
NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *op = [[NSInvocationOperation alloc]
initWithTarget:self
selector:#selector(downloadImage)
object:nil];
[queue addOperation:op];
// The the rest in other methods;
- (void)downloadImage{
NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:person.picURL]];
UIImage* image = [[UIImage alloc] initWithData:imageData] ;
[self showImage: image];
}
I have tried adding the following codes above [self showImage: image]; and i ended up with an exception.
1.) [self performSelectorOnMainThread:#selector(showImage:) withObject:image waitUntilDone:NO];
2.) [self performSelectorInBackground:#selector(showImage:) withObject:image];
// This is the showImage code.
- (void)showImage:(UIImage *)img {
if (img != nil)
{
myImageView = [[UIImageView alloc] initWithImage:img];
myImageView.frame = CGRectMake(20, 20, 132, 124);
[scrollView addSubview:myImageView];
[pictureOfPerson setImage:img];
}
}
I am trying to Asynchronously download the image and cache it. The image gets downloaded Asynchronously but i am not sure if it gets cached.
1.) How to cache the image and use it in when the view loads again
2.) While the image is downloading, what if i click on another view. Then i need to stop the download. How can i write this code. I know that i have to write it in the viewDidDissapear method.
3.) Is my code correct. Have i missed anything or is there a better approach to do this? if so tutorial or some sample code please
I'd use GCD to download the image. It's much simpler to use than NSOperation. Here's an example:
UIImage *personPicture;
personPicture = [self.imageCache objectForKey:person.picURL];
if (!personPicture) {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:person.picURL]];
personPicture = [UIImage imageWithData:imageData];
[self.imageCache setObject:imageData forKey:person.picURL];
dispatch_async(dispatch_get_main_queue(), ^{
[self showImage:personPicture];
});
});
}
else {
[self showImage:personPicture];
}
You could use a class property like an NSMutableDictionary to store the UIImage data and associate it with your URL. If it's present, use it, if not, download the image.
-EDIT-
Adding the code to handle caching.
You can use this:SDWebImage
I'm playing with the tabbar, navigationBar and SegmentedBar in my application.
I have to show an picture from a HTTP like this :
- (void)viewDidLoad {
[super viewDidLoad];
// build the URL, perform the request and show the picture
NSURL *url = [NSURL URLWithString: #"http://api.clementhallet.be/15avril.png"];
//UIImage
[[UIImageView alloc] initWithImage:image];
image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
image.frame = CGRectMake(0, 0, 100, 100);
[self.view addSubview:[[UIImageView alloc] initWithImage:image]];
}
It's running but the picture isn't where I want [exactly][1].
Thanks to help me!
This should be the right order:
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 0, 100, 100);
[self.view addSubview:imageView];
You've got to set the frame of your imageview for positioning. If not in interface builder done, do it like this:
image.frame = CGRectMake(x, y, width, height);
I have written a code for setting imageview ...as per the button clicked...but my problem is when i press the button it is giving me the error like...too many arguments to function ImageNamed...while executing this line...
UIImage* photo = [UIImage imageNamed:#"robort %d.jpg",x];
the whole code is....
-(void)buttonClicked:(UIButton*)button
{
NSLog(#"%d",button.tag);
int x;
x=button.tag;
// Create the image view.
UIImage* photo = [UIImage imageNamed:#"robort %d.jpg",x];
NSLog(#"%d",x);
CGSize photoSize = [photo size];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, photoSize.width, photoSize.height)];
[imageView setImage:photo];
[self.view addSubview:imageView]; }
use UIImage* photo = [UIImage imageNamed:[NSString stringWithFormat:#"robort de nero%d.jpg",x]];
cheers :)
Try this
-(void)buttonClicked:(UIButton*)button
{
NSLog(#"%d",button.tag);
int x;
x=button.tag;
// Create the image view.
NSString *imageName = [NSString stringWithFormat:#"robort %d.jpg",x];
UIImage* photo = [UIImage imageNamed:imageName];
NSLog(#"%d",x);
CGSize photoSize = [photo size];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, photoSize.width, photoSize.height)];
[imageView setImage:photo];
[self.view addSubview:imageView];
}
You need to use:
UIImage* photo = [UIImage imageNamed:[NSString stringWithFormat:#"robort %d.jpg",x]];
Use this
UIImage* photo = [UIImage imageNamed:[NSString stringWithFormat:#"robort %d.jpg",x]];
I have following code in my application:
// to set tip - photo in photo frame
NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:pathOfThumbNail]];
UIImage *cellThumbImg;
if([data length]>0){
cellThumbImg = [UIImage imageWithData:data];
} else {
cellThumbImg = [UIImage imageNamed:#"130X90.gif"];
}
UIImageView *imgView = [[UIImageView alloc]initWithImage:cellThumbImg];
imgView.frame = photoFrame;
[imgBg setContentMode:UIViewContentModeScaleToFill];
[cell.contentView addSubview:imgView];
//[cell.contentView sendSubviewToBack:imgView];
[imgView release];
[data release];
I want the following:
Suppose an image ( which is loaded through nsdata ) is having size of 60 x 60 then content mode should be UIVIewContentModeCenter
Suppose an image ( which is loaded through nsdata ) is having more size then 60 x 60 then content mode should be UIViewContentModeScaleToFill.
But my question is how can I determine the size of the image loaded through NSData?
You can remove the following line because the UIImageView will size itself according to the image used to initialize it:
imgView.frame=photoFrame;
From there you can get the size of the image by taking the size of the UIImageView:
CGRect image_bounds = imgView.frame;
I tried following code & it worked.
// to set tip - photo in photo frame
NSData *data=[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:pathOfThumbNail]];
UIImage *cellThumbImg;
if([data length]>0)
{
cellThumbImg=[UIImage imageWithData:data];
}
else
{
cellThumbImg=[UIImage imageNamed:#"130X90.gif"];
}
UIImageView *imgView=[[UIImageView alloc]initWithImage:cellThumbImg];
imgView.frame=photoFrame;
(cellThumbImg.size.height>60 || cellThumbImg.size.width>60 ) ?
[imgView setContentMode:UIViewContentModeScaleToFill] :
[imgView setContentMode:UIViewContentModeCenter] ;
[cell.contentView addSubview:imgView];
[imgView release]; [data release];