Add some images to ScrollView from some URLs - ios

I want to add 3 images from 3 URLs in ScrollView so how could I do?
Please give me a good tutorial or some explanations!

You can use "AsyncImageView" class files it will load image synchronically.
please refer
https://stackoverflow.com/a/15377082/1713478
in this answer this method call for tableview you can use in for loop and place imageview in scrollview
for putting images vertically
int y = 10;
for (int i = 0; i < (number of images); i++)
{
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(X, y, width, height)];
NSString *imageURL = [NSString stringWithFormat: (image link for i number)];
AsyncImageView *async = [[AsyncImageView alloc]initWithFrame:CGRectMake(0, 0, width, height)];
[async loadImageFromURL:[NSURL URLWithString:imageURL]];
[imageView addSubview:async];
[scrollView addSubview:imageView];
y = y+height+10;
}

You can user PSTCollectionView for gridView kind of display and SDWebImage for lazy loading.

Are you asking how to load remote images OR how to layout them on a scroll view?
1.
UIImage * image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"..."]]];
Wrap it all in dispatch_async or put it in NSOperation to make sure you don't block the main thread.
2.
You can try to use UIPageViewController instead of a UIScrollView

Related

Image slightly distorted in UIScrollView

I have a UIScrollView, inside that are UIImageViews all 198*198pts. The images in this scrollview all seem to be slightly distorted. The edges are more jagged (I am working with png files and the images are cartoons/stickers). In a normal UIImageView not contained in a UIScrollView they look fine. Could anyone give me some pointers to why this might be happening?
UIView *content = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width*[self.stickers count], self.scrollView.frame.size.height)];
for(int i=0; i<[self.stickers count]; i++){
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i*self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
MySticker *sticker = self.stickers[i];
imageView.image=sticker.image;
imageView.contentMode=UIViewContentModeScaleAspectFill;
[content addSubview:imageView];
//NSLog(#"scroll view width is %f",self.scrollView.frame.size.width);
//NSLog(#"ImageView width is %f", imageView.frame.size.width);
}
self.scrollView.contentSize=CGSizeMake(content.frame.size.width ,content.frame.size.height);
[self.scrollView addSubview:content];
EDIT ^ added my scrollview setup above

UICollectionViewCell disappears while scrolling

I'm using a uicollectionview to create a carousel like scroll view. I think the uicollectionview is the best approach with this use case. I was able to render uicollectionviewcell inside uicollectionview but the problems is, when I scroll the uicolectionview the later cells will disappear and all the cells went black in the end. Another issue is that the cell is not loaded until it's boundary is fully inside the Carousel.
this is how I set up my uicollectionviewcell inside cellForItemAtIndexPath delegate method
NSString *urlString = [NSString stringWithFormat:BASE_URL, panoid, heading, pitch];
NSURL *panoUrl = [NSURL URLWithString:urlString];
//WIDTH calculated based on screen size, roughly about three cells per screen.
CGFloat WIDTH = collectionView.frame.size.width / 3;
UIImageView *panoImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 133.3)];
// add padding to imageview
panoImg.bounds = CGRectInset(panoImg.frame, 2, 2);
// do I need GCD to do async process here? the images are all loaded from urls. not sure if this will cause a problem.
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSData *data = [NSData dataWithContentsOfURL:panoUrl];
dispatch_async(dispatch_get_main_queue(), ^{
[panoImg setImage:[UIImage imageWithData:data]];
});
});
seeInsideCollectionViewCell *cell = [_panoCarousel dequeueReusableCellWithReuseIdentifier:#"panoCarouselCell" forIndexPath:indexPath];
cell.frame = CGRectMake(indexPath.item * WIDTH + 4, 0, WIDTH, 133.3);
cell.backgroundColor = [UIColor blackColor];
[cell addSubview: panoImg];
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(4, 4, WIDTH, 15)];
[label setBackgroundColor: [UIColor colorWithRed:0.63 green:0.63 blue:0.63 alpha:0.5]];
[label setFont: [UIFont fontWithName:#"Roboto-Light" size:10.0]];
[label setText: _BussinessViewsNames[indexPath.item]];
[cell addSubview:label];
return cell;
any suggestions, the cells were all loaded, just the loading is not seamless and smooth, I've attached a gif to demonstrate the problem here.
remove this line due to you should not change frame for cell
cell.frame = CGRectMake(indexPath.item * WIDTH + 4, 0, WIDTH, 133.3);
When cell reusable, it already has uiimageview, and you again create new uiimageview and add it than cell will be has several uiimageviews as subview
Move load images in another class or method like
self.imageLoader
-(void)loadImageWithURL:(NSURL *)url successBlock:(void (^)(UIImage *image, id itemId))successBlock failureBlock:(void (^)(NSError *error))failureBlock
and call it in cellForItem method (check id of gotten image)

UIImageView fails to show image

UIImage is not displaying an image. I've created a UIImageView custom class for table cells using:
imgView = [[UIImageView alloc]init];
frame= CGRectMake(boundsX+10 ,0, 50, 44);
imgView.frame = frame;
Then, I tried to load each cell and nothing. But, I am able to read the image url in the log messages.
NSURL *url = [NSURL URLWithString: [dict objectForKey:#"imageAdd"]];
NSData *data = [NSData dataWithContentsOfURL: url];
UIImage * image = [[UIImage alloc] initWithData:[[data copy] autorelease]];
NSLog (#"The image is located at the URL: %#", url);
cell.imgView.image = image;
return cell;
Standard UITableViewCell already contains UIImageView that appears to the left to all your labels if its image is set. You can access it using imageView property:
cell.imageView.image = someImage;
If for some reason standard behavior does not suit your needs (note that you can customize properties of that standard image view) then you can add your own UIImageView to the cell as Aman suggested in his answer. But in that approach you'll have to manage cell's layout yourself (e.g. make sure that cell labels do not overlap image). And do not add subviews to the cell directly - add them to cell's contentView:
// DO NOT!
[cell addSubview:imageView];
// DO:
[cell.contentView addSubview:imageView];
As far as I can see from your code, you never added imgView as a subview of your cell. add the line:
[cell.contentView addSubview:imgView];

How do I create a UIImageView using only code?

I am rather new to Cocoa programming and need some help.
I want to make some image views for units in a game and add them with code, because to put them in the view and make conections in Interface Builder is a lot of work.
I already have found code to create and insert UIImageViews:
UIImageView *image0 =[[UIImageView alloc] initWithFrame:CGRecMake(x,y,w,h)];
image0.image=[UIImage imageNamed:#"image.png"];
[self.view addSubview:image0];
My question is: if I can make an array, then I don't have to write this for every image view, and I put the same image in it?
A nice way to do it would be something like this. This will create 30 image views with the same image.
NSMutableArray *arrayOfImageViews = [NSMutableArray array];
for (int i = 0; i < 30; i++) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"theImage.png"]];
[self.view addSubview:imageView];
[arrayOfImageViews addObject:imageView];
}
How about this:
for(int i=0; i<10; ++i) {
UIImageView *image =[[UIImageView alloc] initWithFrame:CGRecMake(x+i*k1,y*i*k2,w,h)];
image.image=[UIImage imageNamed:#"image.png"];
image.tag = i+1;
[self.view addSubview:image0];
}

ios - SizeToFit on UIImageView not working

I have a UIImageView which loads in images from the documents directory on the ios file system .
The problem is that when I call
[imageView sizeToFit];
It does not work. I think it is because the image hasn't loaded fully at that point and so the sizeToFit part is called before it has the image width & height.
Could someone point me in the direction of a fix please.
Here is my code:
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image = [UIImage imageWithContentsOfFile:[FileOperations getLibraryPath:[NSString stringWithFormat:#"%d.png", listData.imageId]]];
[imageView sizeToFit];
[imageScroll setContentOffset:CGPointMake(0, 0)];
imageScroll.contentSize = imageView.frame.size;
imageScroll.decelerationRate = UIScrollViewDecelerationRateFast;
imageScroll.minimumZoomScale = 1;
imageScroll.maximumZoomScale = 2;
[imageScroll setZoomScale:imageScroll.minimumZoomScale];
[imageScroll addSubview:imageView];
Thanks,
Ashley
Check the image size:
NSLog(#"%#", NSStringFromCGSize(imageView.image.size));
You may want to set imageView frame based on the image size manually, for example, if the image is too large to fit the imageView's superview.
UIImageView *myImage = [[UIImageView alloc]init];
myImage.contentMode = UIViewContentModeScaleAspectFit;
myImage.image = [UIImage imageName:#"image.png"];
[self.view addSubview:myImage];
sizeToFit should work in your situation, but fails if the UIImage is nil.
UIImage's imageWithContentsOfFile: returns a (completely loaded) reference to a UIImage or nil. Are you sure that you're passing the right path?

Resources