Downloading Images Asynchronously - iphone

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.

Related

UIImage+AFNetworking doesn't work

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"]];

why my application getting crash in my iPod?

I am adding UIImageView on UIScrollView but my app getting crash when array count is high.
Here is my code
scr=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 60, 320, 320)];
scr.tag=1000;
[scr setDelegate:self];
[self.view addSubview:scr];
dispatch_queue_t queeee =dispatch_queue_create("raj", 0);
int countt=200;
for(int i=0;i<countt;i++)
{
CGRect rectt=CGRectMake((i%3*90)+30,(i/3*90)+40,80,70);
UIImageView *asyImagee = [[UIImageView alloc]initWithFrame:rectt];
dispatch_async(queeee, ^{
NSData *data=[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Default" ofType:#"png"]];
__block UIImage *imgg=[UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^
{
asyImagee.image=imgg;
imgg=nil;
});
});
asyImagee.tag=i;
[scr addSubview:asyImagee];
asyImagee=nil;
}
[scr setContentSize:CGSizeMake(0, countt/3*110)];
Working fine with int countt=100;
but crash with 200;

How to reload my view in iOS

I have a imageView embedded in a scrollView. The imageView needs to be downloaded from the Internet (through flickr api), so I add a thread to handle this. But what am I supposed to do after I finished downloading the image? How can I reload my imageView?Here's my code.
In fact, it is working fine. but the imageView's size is (0, 0). How can I fix that?
- (void)viewDidLoad
{
[super viewDidLoad];
if (!self.myImage)
{
UIActivityIndicatorView* spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[spinner startAnimating];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:spinner];
dispatch_queue_t downloadQueue = dispatch_queue_create("flickr downloader (photo)", NULL);
dispatch_async(downloadQueue, ^{
NSData* data = [NSData dataWithContentsOfURL:[FlickrFetcher urlForPhoto:self.photo format:FlickrPhotoFormatLarge]];
dispatch_async(dispatch_get_main_queue(), ^{
self.myImage = [UIImage imageWithData:data];
[self.imageView setImage:self.myImage];
[self viewDidLoad];
[self viewWillAppear:NO];
NSLog(#"imageViewSet!");
self.navigationItem.rightBarButtonItem = nil;
});
});
dispatch_release(downloadQueue);
}
else
{
NSString* imageTitle;
if ([[self.photo objectForKey:#"title"] length] > 0)
imageTitle = [self.photo objectForKey:#"title"];
else if ([[self.photo valueForKeyPath:#"description._content"] length] > 0)
imageTitle = [self.photo valueForKeyPath:#"description._content"];
else imageTitle = #"Unknown";
[[self navigationItem] setTitle:imageTitle];
self.scrollView.delegate = self;
self.scrollView.contentSize = self.imageView.image.size;
}
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
double scale = self.imageView.bounds.size.width * 1.0 / self.myImage.size.width;
if (scale > self.imageView.bounds.size.height * 1.0 / self.myImage.size.height)
scale = self.imageView.bounds.size.height * 1.0 / self.myImage.size.height;
NSLog(#"imgview%g, %g",self.imageView.bounds.size.width, self.imageView.bounds.size.height);
NSLog(#"img%g, %g",self.myImage.size.width, self.myImage.size.height);
self.imageView.frame = CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height);
[self.scrollView setZoomScale:scale];
}
The usual way is to call:
[self.imageView setNeedsDisplay];
Edited to Add:
I just realized you said the image view is still (0,0). From the docs :
Setting the image property does not change the size of a UIImageView.
Call sizeToFit to adjust the size of the view to match the image.

The latest iPhone 4 has image display issue

The older 3GS, or iPod 4 are able to show the final tempimage, but on the iPhone 4, I don't see the tempview showing up on screen.
I stepped through it, getting imageData, tempimage allocated, tempview allocated, but the final tempview is not showing up.
-(void) display:(NSData*) imageData
{
tempimage= [[[UIImage alloc] initWithData:imageData] autorelease];
tempview=[[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
if (tempimage) {
[tempview setImage:tempimage];
[self.view addSubview:tempview];
}
try like this..
-(void) display:(NSData*) imageData
{
if(imageData != nil)
{
tempimage= [[UIImage alloc] initWithData:imageData];
}
else
{
tempimage= [UIImage imageNamed:#"something.png"];
}
tempview=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
if (tempimage) {
[tempview setImage:tempimage];
[self.view addSubview:tempview];
}
if(imageData != nil)
{
[tempimage release];
}
[tempview release];
}

UIPageControl is not Displayed

I use the following to display scrollview and pagecontrol
scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 179)];
pageControl=[[UIPageControl alloc] initWithFrame:CGRectMake(0, 179, 320, 20)];
scrollView.delegate = self;
[scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
int index=[[self._parameters objectForKey:#"index"] intValue];
NSString *imageNamePrefix=[[activeAppIdList objectAtIndex:index] objectForKey:#"AppID"];
int noOfScreenShot=[[[activeAppIdList objectAtIndex:index] objectForKey:#"NoOfScreenShot"] intValue];
CGFloat cx = 0;
for (int j=1;j<=noOfScreenShot ; j++) {
UIImage *image =[[UIImage alloc] init];
image= [self loadImage:[[NSString alloc]initWithFormat:#"%#_%d.jpg",imageNamePrefix,j]];
if (image == nil) {
NSString *urlString = [[NSString alloc]initWithFormat:#"http://localhost/MoreApp/images/%#_%d.jpg",imageNamePrefix,j];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
UIImage *temp=[[UIImage alloc]initWithData:imageData] ;
[self saveImage:temp withName:[[NSString alloc]initWithFormat:#"%#_%d.jpg",imageNamePrefix,j]];
image = temp;
[temp release];
[urlString release];
}
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[image release];
imageView.frame = CGRectMake(cx,0,scrollView.frame.size.width,scrollView.frame.size.height);
[scrollView addSubview:imageView];
[imageView release];
cx += scrollView.frame.size.width;
}
NSLog(#"No Of pages..%d",noOfScreenShot);
pageControl.numberOfPages = noOfScreenShot;
pageControl.currentPage = 0;
scrollView.contentSize = CGSizeMake(cx,scrollView.frame.size.height);
NSLog(#"Width:%f",scrollView.frame.size.width);
[ refToSelf.instructionView addSubview:scrollView];
[ refToSelf.instructionView addSubview:pageControl];
[scrollView release];
[pageControl release];
scrollview is ok but pagecontroll didn't showed...
can help??
If the page control's and container's background color is the same (by default white) page control won't be visible.
For anyone who finds this, another silly mistake you can make is to not set the numberOfPages attribute on the UIPageControl, which will also cause it to not display.
My mistake was equally annoying - developing with storyboard in 3.5 inch view, and built on 4 inch device on simulator so it was offscreen. Silly!