what I would like to do is to build a UIScrollView which contains a lots of images, the problem is that I don't want to load all the images to the scroll view at once for performance issues, so if someone can suggest and help me to build a UIScrollView which can add images on runtime, for example when I scroll left I get image and the next image is allocated when needed, paging on.
Use this delegate method for add new image.
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
// called on finger up if user dragged. decelerate is true if it will continue moving afterwards
You can implement UIScrollViewDelegate and use its methods like
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
which tells you about any offset changes in the Scroll View, and then you can load whichever image should appear at that offset at run-time. For that, you would have to keep track of the current offset and the page of your scroll view.
Look into the Apple's PageControl Sample Code
This could be a good starting point
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
float currPos = scroll.contentOffset.x;
BOOL movingRight = ( currPos / scroll.frame.size.width ) > 1 ? YES : NO ;
if (movingRight) {
NSLog(#"RIGHT");
[photo2 setImage:[UIImage imageNamed:#"22.jpg"]];
photo2.frame = CGRectMake(scroll.frame.size.width * 2, 0, scroll.frame.size.width, scroll.frame.size.height);
}else{
NSLog(#"LEFT");
[photo2 setImage:[UIImage imageNamed:#"33.jpg"]];
photo2.frame = CGRectMake(0, 0, scroll.frame.size.width, scroll.frame.size.height);
}
//NSLog(#"%f",currPos);
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
UIImage *img = [photo1 getImage];
[photo1 setImage:[photo2 getImage]];
[self.scroll scrollRectToVisible:CGRectMake(self.scroll.frame.size.width, 0, self.scroll.frame.size.width, self.scroll.frame.size.height) animated:NO];
[photo2 setImage:img];
}
Can i implement both the paging and zooming of imageview in a uiscrollview at the same time.
Yes. You can do it.
In each page of the mainScrollView add a subScrollView containing the imageView. You need to do the following things.
Set maximumZoomScale for subScrollView
[subScrollView setMaximumZoomScale:2.0f]; // You can set any value
This value is calculated based on the size of the image displayed in the imageView.
In the viewForZoomingInScrollView: method of the subScrollView return the imageView
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return imageView;
}
Enable paging in mainScrollView
mainScrollView.pagingEnabled = YES;
You have to write further code to handle paging in the mainScrollView.
I use below code for getting zoom image in the scroll view.
(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return photo;
}
but i have problem when i click on next button and set new image it also zoomed.
so it is possible the the next image is normal size. when i click on next button.
Try reseting the Zoom after the next scroll
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[scrollView zoomToRect:(CGRect)rect animated:(BOOL)animated]
// or
[scrollView setZoomScale:(float)scale animated:(BOOL)animated]
}
I created a UIScrollView with a CATiledLayer for a 10000x8078 image.
Would I place this image in a UIView the zoomScale is always updated according to the contentSize (respectively the size of the image).
With CATiledLayer the contentSize now varies according to the tiles in use.
How can I now determine the correct zoomScale of the UISrcollView?
Thanks
What has worked for me, based on the the ZoomingPDFViewer Sample Code, is to keep tabs on the zoom manually the following way in the UIScrollView delegate:
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
scrollView.scrollEnabled = YES;
// set the new scale factor for the TiledPDFView
pdfScale *=scale;
....
I have UIView in which I am drawing UIImage
- (void)drawRect:(CGRect)rect
{
[myImage drawInRect:rect];
}
This UIView is added on UIScrollView and returned as zoomable view i.e
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return iImageBgView;
}
Problem is coming when I do the zoom. Image which is drawn is getting shown pixelet i.e blur or disturbed. can anyone help me on "How to refresh / redraw this image"
I am manually refreshing / redrawing the image by calling
[self setNeedsDisplay];
but no use. seems like it is taking the previous coordinate and draw as per that instead of updated coordinate.
Thanks,
Sagar
This issue has been resolved by removing this UIView and adding it again on scrollview.
This has been done on
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
// Remove Imageview and add to Scrollview again
}