UIScrollView not scrolling… - uiscrollview

I create i uiscrollview, when i run my app, touch the scrollview but the scrollview does not scroll.
help me!
think you!
The code as:
for (int i=0; i<imageCounts; i++)
{
GalleryProperty *itemVo = [itemList objectAtIndex:i];
//1 button
UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0,20,320,460)];
UIImage *image = [UIImage imageWithContentsOfFile:itemVo.pic];
[imageview setImage:image];
[scrollView addSubview:imageview ];
[imageView release];
}

You have to set the content size:
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 3, scrollView.frame.size.height);

When initializing set scrollView frame size equal to your View Size.
and setContent offset size according to the no of images you're showing in the scrollView.
In your case
UIScrollView *yourScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];//init scroll view
for (int i=0; i<imageCounts; i++)
{
GalleryProperty *itemVo = [itemList objectAtIndex:i];
//1 button
UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0,20,320,460)];
UIImage *image = [UIImage imageWithContentsOfFile:itemVo.pic];
[imageview setImage:image];
[scrollView addSubview:imageview ];
yourScrollView.contentSize=CGSizeMake(320*i, 460);//set content size here
[imageView release];
}

Related

how to show images as a gridview like format in uiscrollview in ios?

its just a week that am into ios devlopement and am facing some problem that i cant show the images in scrolview with gridview format...i dont know how to set the frame size to show images in proper gridview format..
here is my code
that add 5 images in scrollview
for(int i=1;i<6;i++)
{
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(x+0, 0, 320, 460)];
[image setImage:[UIImage imageNamed:[NSString stringWithFormat:#"e1%d.png",i]]];
[scr addSubview:image];
x+=320;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
tapGesture.numberOfTapsRequired = 1;
tapGesture.delegate = self;
image.userInteractionEnabled = YES;
[image addGestureRecognizer:tapGesture];
}
scr.pagingEnabled=YES;
scr.contentSize = CGSizeMake(320*5, 300);
[self.view addSubview:scr];
but all 5 images shows in one horizontal line but i want to show in every line 2 images.
here is the solution, i've implemented it in my app. Here is my code sv_port is my scrollView. Download SDWebImageDownloader class files and import it in your project. Add
relevant framework in your project. like imageIO.framework, QuartzCore
if your images are not fetching from url, then u dont need to use asyncImage view., so dont need to download SDWebImageDownloader.
Now, in your .m add
#import "UIImageView+WebCache.h"
#import "SDImageCache.h"
#import "SDWebImageCompat.h"
//function for adding image in your scrollview
-(void)populateImages
{
int x=6,y=6;
for(UIView * view in sv_port.subviews)
{
if([view isKindOfClass:[UIImageView class]])
{
[view removeFromSuperview]; view = nil;
}
if([view isKindOfClass:[UIButton class]])
{
[view removeFromSuperview]; view = nil;
}
}
for(int p=0;p<[Manufacturer count];p++)
{
UIButton *btnImageButton = [UIButton buttonWithType:UIButtonTypeCustom];
btnImageButton.frame = CGRectMake(x,y,70,70);
[btnImageButton setTag:p];
[btnImageButton addTarget:self action:#selector(nextview:) forControlEvents:UIControlEventTouchUpInside];
UIActivityIndicatorView *spinny = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinny.frame=CGRectMake(btnImageButton.frame.origin.x+25,btnImageButton.frame.origin.y+25, 20, 20);
spinny.backgroundColor=[UIColor clearColor];
[spinny startAnimating];
[sv_port setScrollEnabled:YES];
[sv_port addSubview:spinny];
UIImageView *asyncImageView = [[[UIImageView alloc] initWithFrame:btnImageButton.frame] autorelease];
asyncImageView.backgroundColor=[UIColor clearColor];
CALayer *layer;
layer = asyncImageView.layer;
layer.borderWidth = 0.5;
layer.cornerRadius = 5;
layer.masksToBounds = YES;
[asyncImageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#", yourUrl] ] placeholderImage:[UIImage imageNamed:nil] options:0 andResize:CGSizeMake(btnImageButton.frame.size.width,btnImageButton.frame.size.height) withContentMode:UIViewContentModeScaleAspectFit];
asyncImageView.contentMode = UIViewContentModeScaleAspectFit;
[sv_port addSubview:btnImageButton];
[sv_port addSubview:asyncImageView];
int imgCntPort=0;
imgCntPort=(sv_port.frame.size.width/(asyncImageView.frame.size.width));
//NSLog(#"imgport %d",imgCntPort);
if((p+1)%imgCntPort==0)
{
x=5;
y=y+80;
}
else
{
x=x+80;
}
}
sv_port.contentSize = CGSizeMake(0, y);
glob_x =0; glob_y =y;
}
By this code you will get 4 images in a row., By the change co-Ordiante of x and y you can maintain number of images per row to display.
Hope this helps. Thanks

Xcode: ScrollView & image array

I have an array with images presented in a scrollView. It works perfectly on the simulator however when i run it on the actual device (1st gen iPad) it crashes. At first i lowered the quality of the images to the lowest possible (<120kb each) but that didn't solved my problem. Then i reduced the number of images from 13 to 6 and it works. Is there any work you can think of?
Below is the code i use:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
images = [NSArray arrayWithObjects:[UIImage imageNamed:#"back1.jpg"], [UIImage imageNamed:#"back2.jpg"], [UIImage imageNamed:#"back3.jpg"], [UIImage imageNamed:#"back4.jpg"], [UIImage imageNamed:#"back5.jpg"], [UIImage imageNamed:#"back6.jpg"], [UIImage imageNamed:#"back7.jpg"], [UIImage imageNamed:#"back8.jpg"],[UIImage imageNamed:#"back9.jpg"],[UIImage imageNamed:#"back10.jpg"],[UIImage imageNamed:#"back11.jpg"],[UIImage imageNamed:#"back12.jpg"],[UIImage imageNamed:#"back13.jpg"], nil];
pageControl.numberOfPages=images.count;
for (int i = 0; i < images.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
subview = [[UIImageView alloc] initWithFrame:frame];
subview.contentMode = UIViewContentModeScaleAspectFit;
subview.image = [images objectAtIndex:i]; //problima sthn eikona
[self.scrollView addSubview:subview];
}
pageControlBeingUsed = NO;
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * images.count, self.scrollView.frame.size.height);}

How to stretch (scale) image on a UIView?

How can i take an image and stretch it to fill the screen on my UIView (320/460)
I am loading and placing it, however as you can see, some scaling is needed.
Thank you!
CGRect gameArea = CGRectMake(0, 0, 320, 460);
UIImage *backgroundImage = [UIImage imageNamed:#"green_background.jpg"];
UIImageView *myImageView = [[UIImageView alloc] initWithImage:backgroundImage];
[myImageView setContentMode:UIViewContentModeScaleToFill];
UIView *topView = [[UIView alloc] initWithFrame:gameArea];
[topView addSubview:myImageView];
[[self view] addSubview:topView];
Like I said in my comments above to Shamsiddin's post, the UIImageView needs a size to know where to stretch to. Here I'm using the same size of the UIView. I've added another answer because the code to UIView is missing. So here's the complete code.
// Area of the UIView and UIImageView
CGRect gameArea = CGRectMake(0, 0, 320, 460);
// Create UIImageView with Image
UIImage *backgroundImage = [UIImage imageNamed:#"green_background.jpg"];
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:gameArea];
[myImageView setImage:[backgroundImage stretchableImageWithLeftCapWidth:3 topCapHeight:3]];
[myImageView setUserInteractionEnabled:YES];
// Create UIView and insert subview
UIView *topView = [[UIView alloc] initWithFrame:gameArea];
[topView addSubview:myImageView];
[[self view] addSubview:topView];
CGRect gameArea = CGRectMake(0, 0, 320, 460);
UIImage *backgroundImage = [UIImage imageNamed:#"green_background.jpg"];
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:gameArea];
[myImageView setImage:[backgroundImage stretchableImageWithLeftCapWidth:3 topCapHeight:3]];
[myImageView setUserInteractionEnabled:YES];
[self.view addSubview:myImageView];
try
UIView *view;
view.ContentMode = UIViewContentModeScaleToFill;
I just did this in my own iOS5 app, i needed to dynamically set the background image and scale it. Imagine you are doing this in - (void)viewWillAppear:(BOOL) animated
UIImageView *imageview = [[UIImageView alloc] initWithFrame:self.view.frame];
[imageview setContentMode:UIViewContentModeScaleAspectFill];
[imageview setImage:[UIImage imageNamed:#"Background.png"]];
[self.view addSubview:imageview];
[self.view sendSubviewToBack:imageview];
So we create the view, set it's scaling mode, add the bg image, set it as the subview in the main view and then push it to the back so it does not occupy any other subviews.
Please accept this answer.

Custom scroll view - image outside the frame

I would like to make an infinite scrolling view for iPad. The scrolling works, only when I add pictures to my customized scrollview class, it appears out of the view's frame too (so I have view, which has a UIScrollView on it. This scroll view is connected with my CustomScroll.h :UIScrollView and CustomScroll.m files). Why do the images appear outside the view's frame (the view's frame is set in nib file to 320x420)?
CustomScroll.h:UIScrollView
A part from CustomScroll.m file:
- (id)initWithCoder:(NSCoder *)aDecoder{
if ((self = [super initWithCoder:aDecoder])){
self.contentSize = CGSizeMake(2000, 416);
[self setShowsVerticalScrollIndicator:NO];
for (int i=1; i<=4; i++){
[self addImageAtPosition:i];
}
//todo: to replace with the method: addFirstElementToTheEnd
UIImageView *image1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image1.jpg"]];
[image1 setFrame:CGRectMake(5*320, 0, 320, 416)];
[self addSubview:image1];
[image1 release];
//todo: to replace with the method: addLastElementToTheBeginning
UIImageView *image2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image4.jpg"]];
[image2 setFrame:CGRectMake(0*320, 0, 320, 416)];
[self addSubview:image2];
[image2 release];
}
return self;
}
- (void)addImageAtPosition:(NSInteger)position{
UIImageView *tempImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:#"image%d.jpg", position]]];
[tempImage setFrame:CGRectMake((position * 320), 0, 320, 416)];
[self addSubview:tempImage];
[tempImage release];
}
- (void)recenterIfNecessary{
CGFloat currentOffsetX = [self contentOffset].x;
if (currentOffsetX > 1600.00){
self.contentOffset = CGPointMake(320, [self contentOffset].y );
}
if (currentOffsetX < 320.00){
self.contentOffset = CGPointMake(5*320, [self contentOffset].y);
}
}
- (void) layoutSubviews{
[super layoutSubviews];
[self recenterIfNecessary];
}
TY.

Xcode: Scrollview with images in scrollview

I'm trying to make a view looking almost like the info-view in appStore, with text on the first half of the view and images on the second half. I've tried to use a scrollview in another scrollview. The first view (containing a textview on the top half and a scrollview on the bottom half) scrolls nice, but the scrollview at the bottom (containing several imageviews) doesn't scroll.
Any ideas how to make the second scrollview to scroll?
scrollView is the view containing a textview and a scrollview.
imageScrollView is the view with the imageviews.
[scrollView setContentSize:CGSizeMake(320, 585)];
[imageScrollView setContentSize:CGSizeMake(1520, 400)];
I recently figured it out myself, but forgot to post the answer. The problem was that I set the sizes of the scrollviews in .xib to the same sizes as in the code. When I changed it to 320,416 and 320,400 I was able to scroll them with the setContentSize. In the imageScrollView I then added images along the x-axis (first image at 0, second at image width + a small space and so on). Hope this could help someone.
HERE IS CODE OF scroll-view display images from Documents Directory ByType .png Images who seved by user from Iphone Photo Gallery
- (void)viewDidLoad {
int scrollviewwidh = 120;
self.view.backgroundColor = [UIColor darkTextColor];
hiImage = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 300, 240)];
hiImage.backgroundColor = [UIColor blackColor];
hiImage.userInteractionEnabled = YES;
[hiImage.layer setBorderColor:[UIColor blackColor].CGColor];
[hiImage.layer setBorderWidth:2.01f];
[self.view addSubview:hiImage];
[hiImage release];
scroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 260, 320, 110)];
[self.view addSubview:scroll];
scroll.showsHorizontalScrollIndicator = NO;
scroll.pagingEnabled=YES;
scroll.delegate=self;
scroll.contentSize=CGSizeMake(scrollviewwidh,80);
scroll.showsVerticalScrollIndicator =YES;
scroll.backgroundColor = [UIColor darkTextColor];
}
-(void) viewWillAppear:(BOOL)animated{
self.fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
self.documentsDir = [paths objectAtIndex:0];
NSLog(#"the ----------- %#",documentsDir);
for (UIButton * img in self.scroll.subviews) {
[img removeFromSuperview];
img = nil;
}
int xoffcet = 0;
int col = 0;
int scrollviewwidh = 120;
for (NSString* fileName in [self.fileManager contentsOfDirectoryAtPath: self.documentsDir error:nil]){
if ( [fileName rangeOfString:#".png"].location != NSNotFound ) {
NSLog(#"add %#", fileName);
UIImage* img = [UIImage imageWithContentsOfFile:
[self.documentsDir stringByAppendingPathComponent:fileName]
];
imgView = [[UIButton alloc] init];
[imgView setImage:img forState:UIControlStateNormal];
[imgView addTarget:self action:#selector(imageViewClicked:) forControlEvents:UIControlEventTouchUpInside];
[imgView.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[imgView.layer setBorderWidth:1.0f];
imgView.bounds = CGRectMake(10,10, 50, 50);
imgView.frame = CGRectMake(5+xoffcet, 0, 160, 110);
scroll.contentSize =CGSizeMake(scrollviewwidh+xoffcet,110);
[scroll addSubview:imgView];
[imgView release];
xoffcet +=170;
}
if (col++>1) {
//row++;
col = 0;
}
}
self.title = #"saved Photo";
// [scroll release];
[super viewDidLoad];
// scroll.pagingEnabled = YES;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:
UIBarButtonSystemItemCancel
target:self
action:#selector(photogallary:)];
}
-(IBAction)imageViewClicked:(UIButton *)sender{
for (NSString* fileName in [self.fileManager contentsOfDirectoryAtPath: self.documentsDir error:nil]){
if ( [fileName rangeOfString:#".png"].location != NSNotFound ) {
NSLog(#"add %#", fileName);
UIImage* img = [UIImage imageWithContentsOfFile:
[self.documentsDir stringByAppendingPathComponent:fileName]];
hiImage.image = img;
}
}
}
All The Very Best i Hope its useful Thank You If Thats code Help please give Your Feedback

Resources