how load many photos from url in background (asynchronous) - objective-c

i have this method i use to load many images to scroll view, but when the images load from the url my scroll view is stuck and i can't understand how to load them in the background so the user will not feel it.
this method will call few times (8) in "for" cycle.
- (void)loadPhotosToLeftscroll{
//getting image information from JSON
NSMutableDictionary *photoDict;
photoDict = [leftPhotoArray lastObject];
//getting the photo
NSString *photoPath = [photoDict objectForKey:#"photos_path"];
NSLog(#"photo Path:%#",photoPath);
NSData * imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:photoPath]];
UIImage *image = [UIImage imageWithData:imageData];
// use the image how you like, say, as your button background
//calculating the hight of next photo
UIImageView *leftImage = [leftBlockScroll.subviews lastObject];
//allocating photoView
UIImageView *photoView = [[UIImageView alloc]initWithFrame:CGRectMake(5 , leftImage.frame.origin.y + leftImage.frame.size.height+5, image.size.width/2, image.size.height/2 )];
photoView.userInteractionEnabled=YES;
[photoView.layer setMasksToBounds:YES];
[photoView.layer setCornerRadius:3];
//getting items list
NSDictionary *sh_items = [photoDict objectForKey:#"items"];
//adding image button
UIButton *imageOverButton = [UIButton buttonWithType:UIButtonTypeCustom];
imageOverButton.frame = CGRectMake(0, 0, photoView.frame.size.width, photoView.frame.size.height);
[imageOverButton addTarget:self action:#selector(LeftimagePressed:) forControlEvents:UIControlEventTouchUpInside];
[imageOverButton setTag:[leftPhotoArray count]-1];
[photoView addSubview:imageOverButton];
//adding sh button to imageView
[self addThe_sh_signs:sh_items To_ImageView:photoView];
//subViewing the image to the scrollView
[self insert_Image:image toImageView:photoView in_Scroll:leftBlockScroll];
//calclulating the position of next imageView in scroll.
nextLeftPhotoHight = photoView.frame.size.height + photoView.frame.origin.y + 5;
//calculating the hight of the highest scroll view in both.
leftBlockScroll.contentSize = CGSizeMake(160, [self theSizeOfScrollViewHight]);
rightBlocScroll.contentSize = CGSizeMake(160, [self theSizeOfScrollViewHight]);
isLoadindContant = NO;
[self.view reloadInputViews];
[leftBlockScroll reloadInputViews];
}
please do not send me to some link that trying to explain how to use the asynchronous.
Try to explain according the method you see here.
Im here for any question, that you need to ask to help me.

You will have to do it asynchronously in a proper way. I do not think there is any way around that. I subclassed an UIImageView object and placed many instances of it within the cells of a talbe (in your case within the scroll view). The subclass objects are initialized with an url and load their image asynchronously (with some caching so that the image is not loaded every time).
This tutorial helped me much in the beginning:
http://www.markj.net/iphone-asynchronous-table-image/
You will just have to adopt that to your scroll view. The underlaying principle remains the same.

Related

Having 2 UIimageviews load the same pic

I have 2 UIImageviews that I would like to share the same pic.png when the view controller loads. How do I link the UIImageviews to show the same pic?
It's simple
imageView1.image = [UIImage imageNamed:#"test.png"];
imageView2.image = imageView1.image;
Hope it helps you..
The most straightforward way is as follows:
self.imageView1.image = myImage;
self.imageView2.image = myImage;
If you want them linked so they always show the same image, you’re best off putting making a method like this. I’ve put the UIImageViews in an array, to show how you could expand this technique to as many image views as you want:
- (void)showImage:(UIImage *)newImage
{
for (UIImageView *imageView in imageViewsArray)
{
imageView.image = newImage;
}
}
Just set both UIImages to the same thing, or just give the second image the value of the first.
imageView.image = [UIImage imageNamed:#"Default.png"];
imageView2.image = imageView.image;

Adding individual UIImageViews for each item in an array

I am trying to add a individual UiImageView for each item in an array this is what I have so far
_shapeArray = [NSArray arrayWithObjects:#"cloud.png",#"star.png", nil];
for (int i = 0; i < [_shapeArray count]; i++){
// I make a UIImage, which I will draw later
UIImage* image = [UIImage imageNamed:[NSString stringWithFormat:#"%#",[_shapeArray objectAtIndex:i]]];
UIImageView* blockView = [[UIImageView alloc] initWithImage:image];
blockView.frame = CGRectMake(arc4random()%320, arc4random()%460, image.size.width, image.size.height);
[self.view addSubview:blockView];
}
But as you can tell it just adds the last image in the array. I can not figure out a way to maybe add the array object number to the name of the UIImageView. Maybe I am going at it the wrong way, if so what would be the best way?
This code works, but you need to make sure of a couple of things:
- That the file name actually exists in your bundle (check for uppercase/lowercase), you would not get an error message if it didn't, but it would not show the picture
- That the image sizes are not too large and don't cover each other
You are adding the images in the same frame.
blockView.frame = CGRectMake(arc4random()%320+SomeXValue, arc4random()%460+SomeYvalue, image.size.width, image.size.height);

ios tableviewcell several objects one a cell programatically , setting prototype cell programatically , custom table view cell style,

hello I built this tableview using the InterfaceBuilder and storyboard I used custom for the style of the cell and content prototype of the table view, this is the image:
custom tableview
but now I need to redo everything programmatically I tried insert an imageview in the cell using this code
UIImageView *imgView = [[UIImageView alloc] initWithFrame:
CGRectMake(300, 0, 80, 80)];
imgView.image = [UIImage imageNamed:#"esquina_estrella.png"];
cell.imageView.image = imgView.image;`
but the image stays static unresponsive to CGRectMake thanks for you help
Use this code to the Cell:
// Make your frame origin 0,0 as below done.
UIImageView *imgView = [[UIImageView alloc] initWithFrame:
CGRectMake(0, 0, 80, 80)];
imgView.image = [UIImage imageNamed:#"esquina_estrella.png"];
[cell addSubview:imgView];
[imgView release];
imgView = nil;
Add
imgView.clipsToBounds = YES;
To limit display of the image to the frame of the image view.
Also, you are adding the image of the image view to the cell's image view, which is a different object, without using your image view and its frame. You actually do not need an image view if you are just using the image. What you want is
cell.imageView = imgView;
or
[cell.contentView addSubview:imgView];
The latter will be preferable if you want to place it exactly and add more image views as in your screen shot.

Loading a png into a UIImageView iOS

I have two images that need to be overlaid over one another, and they are both png images (since I need to be able to make them transparent). However, when I load them into a UIImage view in my xib file, neither of them display at all! When I try using the jpg format of the same images it works fine, but because jpg doesn't support transparency, the overlay effect I need is lost. How can I get the png images to actually display in the window?
This is the kind of task that is easier to do from code than from Interface "Crappy" Builder:
CGRect imageFrame = CGRectMake(x, y, width, height);
UIImage *image1 = // however you obtain your 1st image
UIImage *image2 = // however you obtain your 2nd image
UIImageView *imgView1 = [[UIImageView alloc] initWithImage:image1];
// Adjust the alpha of the view
imgView1.alpha = 1.0f; // This is most advisably 1.0 (always)
imgView1.backgroundColor = [UIColor clearColor];
imgView1.frame = imageFrame;
UIImageView *imgView2 = [[UIImageView alloc] initWithImage:image2];
// Adjust the alpha of the view
imgView1.alpha = 0.5f; // or whatever you find practical
imgView1.backgroundColor = [UIColor clearColor];
imgView2.frame = imageFrame;
// Assume a view controller
[self.view addSubview:imgView1];
[self.view addSUbview:imgView2]; // add the image view later which you wanna be on the top of the other one
// If non-ARC environment, we need to take care of the percious RAM
[imgView1 release];
[imgView2 release];
Try to open your png images in a photo editor like photoshop or pixelmator and save it again as NOT interlaced (in the save options of png).

Animation on button using images

I have a UIButton which has arrow kind of blue image set as
UIImage *arrowImage = [UIImage imageNamed:#"bluearrow.png"];
[self.prevbutton setBackgroundImage:arrowImage forState:UIControlStateNormal];
Now I have another image yellow.png, that I want to super impose on top of this button image and make a flashing animation continuously. How can I do this?
This should help you.
myButton.imageView.animationImages =
[NSArray arrayWithObjects:[UIImage imageNamed:#"image1.png"],
[UIImage imageNamed:#"image2.png"],
nil];
myButton.imageView.animationDuration = 0.5; //whatever you want (in seconds)
[myButton.imageView startAnimating];
As quoted from https://stackoverflow.com/a/7490535/716216