Images are not properly displaying on UIScrollView - ios

I have images in a UIScrollView, but the images are not displaying properly. The problem is that images are not at the same size. I want every image with the same size and height.
for (UIView *v in [_scrollView subviews]) {
[v removeFromSuperview];
}
//CGRect workingFrame = _scrollView.frame;
CGRect workingFrame = CGRectMake(0, 0, 200, 134);
workingFrame.origin.x =0;
NSMutableArray *images = [NSMutableArray arrayWithCapacity:[info count]];
for(NSDictionary *dict in info) {
UIImage *image = [dict objectForKey:UIImagePickerControllerOriginalImage];
[images addObject:image];
UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
imageview.frame = workingFrame;
[_scrollView addSubview:imageview];
[imageview release];
workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;
}
self.chosenImages = images;
NSLog(#"values=%#",_chosenImages);
[_scrollView setPagingEnabled:YES];
[_scrollView setContentSize:CGSizeMake(workingFrame.origin.x, workingFrame.size.height)];

If you set UIViewContentModeScaleAspectFit you will have the image with the maximum size of the UIImageView, but often smaller. If you want it to fill and have all the same size and keep aspect you have to set:
[imageview setContentMode:UIViewContentModeScaleAspectFill];
[imageview setClipsToBounds:YES];

Related

ios How to add UIImage to UITextView asynchronous?

I can add image asynchronously seperately by using UIImageView+AFNetworking
[imageView setImageWithURL:[NSURL URLWithString:[self.data valueForKey:#"image_link"]]];
I can also add image to UITextView by
UITextView *text = [[UITextView alloc] init];
UIImageView *imageView = [UIImageView new];
[imageView setImage:[UIImage imageNamed:#"image.png"]];
CGRect aRect = CGRectMake(0, 0, 125, 120);
[imageView setFrame:aRect];
UIBezierPath *exclusionPath = [UIBezierPath bezierPathWithRect:aRect];
text.textContainer.exclusionPaths = #[exclusionPath];
[text addSubview:imageView];
But I cannot add image asynchronously in UITextView like
UITextView *text = [[UITextView alloc] init];
UIImageView *imageView = [UIImageView new];
[imageView setImageWithURL:[NSURL URLWithString:[self.data valueForKey:#"image_link"]]];
CGRect aRect = CGRectMake(0, 0, 125, 120);
[imageView setFrame:aRect];
UIBezierPath *exclusionPath = [UIBezierPath bezierPathWithRect:aRect];
text.textContainer.exclusionPaths = #[exclusionPath];
[text addSubview:imageView];
So how can I add image asynchronously in UITextView?
Use a dispatch :
dispatch_async(dispatch_get_main_queue(), ^{
// async UI update here
});

Z position of UIImageView to that of UIScrollView

I have both UIScrollView and another UIImageView. I want the UIImageView to come over the UIScrollView. I've tried all the bringSubviewToFront and the insertAtIndex stuff but its not working. Please help me out!
Here is the code -
For UIScrollView:
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 300,
SCREEN_WIDTH, SCREEN_HEIGHT)];
scrollView.showsHorizontalScrollIndicator = FALSE;
[self.view addSubview:scrollView];
__block int tagValue = 1;
__block NSInteger tag = 1;
for (int i=0; i<[listOfImages count]; i++) {
NSDictionary *myDic = [listOfImages objectAtIndex:i];
NSString *urlImage = [myDic objectForKey:#"product_image"];
//NSLog(#"%lu",(unsigned long)[listOfImages count]);
image = [[UIImageView alloc] initWithFrame:CGRectMake(leftMargin, 0, 200, 140)];
// [image setImage:[UIImage imageNamed:#"img_def.png"]];
NSURL *imageURL = [NSURL URLWithString:urlImage];
[image setImageWithURL:imageURL
placeholderImage:[UIImage imageNamed:#"img_def.png"]];
image.tag = tag;
image.contentMode = UIViewContentModeScaleAspectFit;
[scrollView insertSubview:image atIndex:1];
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleTap:)];
recognizer.numberOfTapsRequired = 1;
recognizer.numberOfTouchesRequired = 1;
recognizer.delegate = self;
[image addGestureRecognizer:recognizer];
[image setUserInteractionEnabled:YES];
leftMargin += SCREEN_WIDTH;
tagValue += 1;
tag += 1;
}
[scrollView setContentSize:CGSizeMake(leftMargin, 0)];
[scrollView setPagingEnabled:YES];
And the image code that I want to come on top of the scroll view -
UIImage *originalPromo = [UIImage imageNamed:#"promo"];
UIImage *scaledPromo = [UIImage imageWithCGImage:[originalPromo CGImage]
scale:(originalPromo.scale *2.0) orientation:(originalPromo.imageOrientation)];
UIImageView *promo = [[UIImageView alloc] init];
[promo setFrame:CGRectMake(45.5, 300.0,
scaledPromo.size.width, scaledPromo.size.height)];
[promo setImage:scaledPromo];
[self.view insertSubview:promo atIndex:100];
Instead of using insertSubview:atIndex use addSubview:. When a subview is added, it's always added on top of all other subviews already in the parent view.

UIScrollView With Pictures Generated

I have set up a UIScrollView and loaded in pictures and set it equal to the view with some offset between the pictures. Can someone possibly explain what I might have done wrong? It shows the first picture just fine but wont let me scroll left and right to see the next ones. Do I need a gesture recognizer with the new XCode to make this work?
- (void)viewDidLoad
{
[super viewDidLoad];
int PageCount = 3;
UIScrollView *Scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
Scroller.backgroundColor = [UIColor clearColor];
Scroller.pagingEnabled = YES;
Scroller.contentSize = CGSizeMake(PageCount = Scroller.bounds.size.width, Scroller.bounds.size.height);
CGRect ViewSize = Scroller.bounds;
UIImageView *ImgView = [[UIImageView alloc] initWithFrame:ViewSize];
[ImgView setImage:[UIImage imageNamed:#"1.png"]];
[Scroller addSubview:ImgView];
ViewSize = CGRectOffset(ViewSize, Scroller.bounds.size.width, 0);
UIImageView *ImgView2 = [[UIImageView alloc] initWithFrame:ViewSize];
[ImgView2 setImage:[UIImage imageNamed:#"2.png"]];
[Scroller addSubview:ImgView2];
ViewSize = CGRectOffset(ViewSize, Scroller.bounds.size.width, 0);
UIImageView *ImgView3 = [[UIImageView alloc] initWithFrame:ViewSize];
[ImgView3 setImage:[UIImage imageNamed:#"3.png"]];
[Scroller addSubview:ImgView3];
[self.view addSubview:Scroller];
}
#end
You need to manually set the contentSize of your ScrollView to the maximum rectangle which fits all the ImageViews you added. For example : Lets say you want to scroll right and left and you have 4 views with each view having width 100 and you have offset of 10 in x direction. Then after adding all the 4 views to your scrollView, you will have to make the contentSize as below :
scrollView.contentSize = CGSizeMake( 10 + (100 + 10)*4 , scrollView.contentSize.y );
This will make the scrollView scrollable and you will see all the views.
Try this
int PageCount = 3;
NSMutableArray *arrImageName =[[NSMutableArray alloc]initWithObjects:#"1.png",#"2.png",#"3.png", nil];
UIScrollView *Scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
Scroller.scrollEnabled=YES;
Scroller.backgroundColor = [UIColor clearColor];
Scroller.pagingEnabled = YES;
[self.view addSubview:Scroller];
int width=Scroller.frame.size.width;
int xPos=0;
for (int i=0; i<PageCount; i++)
{
UIImageView *ImgView = [[UIImageView alloc]initWithFrame:CGRectMake(xPos, 0, Scroller.frame.size.width, Scroller.frame.size.height)];
[ImgView setImage:[UIImage imageNamed:[arrImageName objectAtIndex:i]]];
[Scroller addSubview:ImgView];
Scroller.contentSize = CGSizeMake(width, 0);
width +=Scroller.frame.size.width;
xPos +=Scroller.frame.size.width;
}

Scrollview not shwoing images

i have retrived images from document directory and want to display that images in scrollview. the images is coming from the document directory but not displaying in scrollview. i have the below code. I have put scrollview graipically on xib and also creat outlet..
self.scrollView.contentSize = CGSizeMake(320, 136);
self.scrollView.delegate = self;
int X=0;
for (int i = 0; i < [imageFileNames count]; i++)
{
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%d/%d.png",b, i]];
UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];
[images addObject:img];
NSLog(#"%#",getImagePath);
//imge.image=img;
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(X, 0, 100, 100)] ;
[imageView setImage: [UIImage imageNamed:[NSString stringWithFormat:#"%d.png", i]]];
[self.scrollView addSubview: imageView];
X = X + imageView.frame.size.height+5;
if(X > 320)
self.scrollView.contentSize = CGSizeMake(X, 140);
}
Seems you are trying to create the UIImageView from images that are located in your resources (but they are not there!).
[imageView setImage: [UIImage imageNamed:[NSString stringWithFormat:#"%d.png", i]]];
whats the point of placing the images in a array, if you dont use it?
try this instead:
[imageView setImage: [images objectAtIndex:i]];

IOS: add some sub-scrollview in a scrollview, image don't appear

I have this code:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *image = [NSArray arrayWithObjects:[UIImage imageNamed:#"1.png"], [UIImage imageNamed:#"2.png"], [UIImage imageNamed:#"3.png"], nil];
for (int i = 0; i < image.count; i++) {
CGRect frame;
frame.origin.x = scrollV.frame.size.width * i;
frame.origin.y = 0;
frame.size = scrollV.frame.size;
UIScrollView *subScrollView = [[UIScrollView alloc] initWithFrame:frame];
UIImageView *subview = [[UIImageView alloc] initWithFrame:frame];
[subview setImage:[image objectAtIndex:i]];
[subScrollView addSubview:subview];
[scrollV addSubview:subScrollView];
[subview release];
[subScrollView release];
}
scrollV.contentSize = CGSizeMake(scrollV.frame.size.width * image.count, scrollV.frame.size.height);
}
scrollV is the main scrollview and it have "paging enabled"
if I use this code I have my scrollV with paging enabled but I see inside it only "1.png" at first page, I don't see others png, why?
You're settings the frame of subview to the incorrect value. That is making it the same frame as its subScrollView so it's pushing it off to the right. Try this:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *image = [NSArray arrayWithObjects:[UIImage imageNamed:#"1.png"], [UIImage imageNamed:#"2.png"], [UIImage imageNamed:#"3.png"], nil];
for (int i = 0; i < image.count; i++) {
CGRect frame;
frame.origin.x = scrollV.frame.size.width * i;
frame.origin.y = 0;
frame.size = scrollV.frame.size;
UIScrollView *subScrollView = [[UIScrollView alloc] initWithFrame:frame];
UIImageView *subview = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, frame.size.width, frame.size.height)];
UIImage *image = [image objectAtIndex:i];
subScrollView.contentSize = image.size;
[subview setImage:image];
[subScrollView addSubview:subview];
[scrollV addSubview:subScrollView];
[subview release];
[subScrollView release];
}
scrollV.contentSize = CGSizeMake(scrollV.frame.size.width * image.count, scrollV.frame.size.height);
}
Edit: Also probably best to set the content size of the inner scrollview. I added code to that effect above.

Resources