a problem to load imageView to scrollView - ipad

I am an new user of Objective-c. I have a problem to load imageview to scrollview.
I use interface builder to add a scrollview onto view. and then I try to add an imageview on the scrollview by code.
UIImage *image = [[[UIImage alloc]init]autorelease];
image = [UIIamge imageNamed:#"cover.jpg"];
imageView = [[[UIImageView alloc]init]autorelease];
imageVIew.frame = CGRectMake(50.0, 40.0, 200.0,200.0);
imageView.image = image;
[scorllView addSubview:imageView];
then I add another imageView onto the ScrollView with position at (50.0,1000.0)
and length = 200, width = 200 (the screen of ipad is 786*1004)
the photos can appear on the screen. The second photo is not complete, and I try to scroll the screen, however I can't scroll it.
Thanks.

first, you only need
UIImage * image = [UIImage imageNamed:#"cover.jpg"];
class methods(imageNamed: here) that creates the instance of it other than "alloc" would create a autoreleased object.
[UIImage imageNamed:#"cover.jpg"]
is conceptually identical to
[[UIImage alloc] initWithFileContents:#"cover.jpg"] autorelease] // note, alloc once, release once.
(it may not be identical internally, imageNamed: caches the image initWithFileContents: might not)
In order to scroll the scrollView,
there are many things you need to make sure.
One of it is to make sure your scrollView's contentSize is bigger than your scrollView's frame size.
try setting your scrollView.contentSize = CGSizeMake(someBigValue, someBigValue);
The content inside scrollView scrolls inside the scrollView. So your content should be bigger than scrollView's frame size to scroll.

you have to increase contentinset or UIScrollview to make it scrollable,
You can increase bottom,
scorllView.contentInset = UIEdgeInsetsMake(0, 0, 680, 0);
or increase as you need

Related

Image and Html content on app screen with combined scrolling

Problem
I want to display an image and Html text on the app screen. The text is large so it may not fit on a screen so I want the content to be scrollable. WebView by default has scroll functionality for content going beyond screen size. But I don't want just the text to move upwards as I scroll down but also the image to move upwards i.e. I do not want the image to stick around on the screen when I scroll downwards.
One simple solution for this is to have the image rendered within the html content, but I do not want to do that because ImageView allows me additional functionality that I want to use.
Possible solution
The solution I tried for this was having an ImageView and a WebView within a ScrollView but when I try implementing it, only the webview content is scrolling and the image is fixed. (This is possibly because the WebView's scroll is getting preference over the parent ScrollView's scroll. I even tried setting WebView's child scroll view's delegate to outer scroll View but that didn't help and html text was not scrollable at all after build:
webview.scrollView.delegate = self.scrollView
How can I solve the problem of combined scrolling for ImageView and content in WebView possibly using other container views (if necessary)?
It sounds like you could use a UITableView with a UIImageView as your tableViewHeader. The UIWebView component could be a UITableViewCell or a tableFooterView depending on how you want to architect your view.
Set your UIImageView as your UITableView's tableHeaderView. That way, as the table scrolls, your image will scroll off screen.
Create a UITableViewCell that contains a UIWebView. You can set the height of the cell to whatever your content size is, or the screen size. The webview itself will be scrollable in this cell, and the user also is able to scroll back up to see the image header.
You can try this method :
Disable the scrolling of webview. Place the image on top of webview.
self.yourScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
self.yourWebView = [[UIWebView alloc] initWithFrame:CGRectMake(x position, y position, width, height)];`
self.yourWebView.opaque = NO;
self.yourWebView.scrollView.scrollEnabled = NO;
self.yourNewsArticleImageData = ({
UIImageView* imgView = [[UIImageView alloc] initWithFrame:CGRectMake(x position, y position, width, height)];
imgView.contentMode = UIViewContentModeScaleAspectFit;
imgView.backgroundColor = [UIColor clearColor];
imgView;
});
[self.yourScrollView addSubview:self.yourWebView];
[self.yourScrollView addSubview:self.yourNewsArticleImageData];
self.yourScrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
self.yourScrollView.scrollEnabled = YES;
[self.view addSubview:self.yourScrollView];
Main thing you need to do is disable the scrolling of webview and enable scrolling of your scroll view. Put image and webview content as subviews of scrollview.
You can change the height of the scroll view in the webview didFinish delegate method. Once you get the height of the webView loaded content change the "content size" of scroll view accordingly.

How to use UIScrollView

I am trying to achieve something similar to the photo options bar in VSCO Cam. I tried setting it up with an image, just to get an idea on how to do it, but its not working.
I think one of the problems could be with the fact that the UIScrollView should be horizontal.
Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.scrollView setContentSize:CGSizeMake(646, 73)];
self.scrollView.scrollEnabled = YES;
UIImage *bleh = [UIImage imageNamed:#"Digits 2 1"];
UIImageView *raaaa = [[UIImageView alloc]initWithImage:bleh];
[self.scrollView addSubview:raaaa];
}
So the first thing you need to understand is the contentsize property.
The subviews inside the scrollview are its "content". They can be arranged in any way you want inside the view.
In order to make it scroll though, you need to let the scrollview know how much "space" its content is taking up.
In your case, I'm going to guess that your image is 646 x 73 in dimensions. If you set the contentsize to that, the scrollview doesn't need to scroll because it can show all the content at once. However when you increase it, the scrollview now knows there is more content outside of the screen and it will allow you to scroll.
The second thing you need to understand is the position of the subviews. If you set the frame of your imageview to (0,0,646,73), it will be in the "left-most" position inside the scrollview. Thus, it will only scroll to the right (the white space you see).
Try this code and see if you can understand whats happening:
[self.scrollView setContentSize:CGSizeMake(1292, 73)];
UIImage *im = [UIImage imageNamed:#"Digits 2 1"];
UIImageView *v1 = [[UIImageView alloc]initWithImage:im];
UIImageView *v2 = [[UIImageView alloc]initWithImage:im];
v1.frame = CGRectMake(0,0,646,73);
v2.frame = CGRectMake(646,0,646,73);
[self.scrollView addSubView:v1];
[self.scrollView addSubView:v2];

Image in UIScrollView changes position after scrolling the table

I have a UITableView Cell that contains a UIScroller that has a UIImage in it.
I am having an issue where by when I first load the view, everything looks OK, but when I scroll up and down it changes.
Here is a screen shot of before:
And here is one after I do a scroll:
My code that loads the UIImage in the UIScroller is:
UIImageView *imageview = [[UIImageView alloc] initWithImage:myImage];
imageview.frame = self.scrollview.bounds;
self.scrollview.delegate = self;
[self.scrollview addSubview:imageview];
Can someone give me some pointers? I tried using code from the following stack overflow posts but they have not been able to do the trick so far:
UIImage Is Not Fitting In UIScrollView At Start
UIScrollView with centered UIImageView, like Photos app
Any suggestions?
Thanks
Setting the contentSize of your scrollview to the dimensions of your imageview might help.

UIView subview not resizing

I am working on a Universal Application. I have a View-based application which has one view controller. I am adding a UIImageView as a subview.
My problem is that if I put the ImageView in viewDidLoad, it is getting resized in iPad.
But if I add the ImageView to view controller dynamically on button tap event, the UIImageView is not getting resized as per iPad. Instead, it is showing ImageView with 320 x 480 dimensions.
Note: My View Controller has setAutoresizesSubviews to YES.
My code is as below:
-(void)buttonTap
{
UIImage *img = [[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:#"main-bg" ofType:#"png"]];
UIImageView *imgViewMainBG = [[UIImageView alloc]initWithImage:img];
imgViewMainBG.frame = CGRectMake(0,0,320,480);
imgViewMainBG.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:imgViewMainBG];
[imgViewMainBG release];
[img release];
img=nil;
}
AutoresizingMask is for changing the size of all subviews according to the change in size of the superView whenever the superView's size is changed, it won't resize subviews at the time of adding a subView.
But you can find the bounds or frame of self.view and set the frame property of imageView according to that bounds like below
[imgViewMainBG setFrame:CGRectMake(0, 0, self.view.frame.size.width,
self.view.frame.size.height)];
so that imgViewMainBG is fully visible in ipad
Try changing image view's contentMode property to UIViewContentModeScaleToFill.

Resizing subview with superview's frame change

I have a UIImageView aSuper; This view is changing its sizes when i flip it i.e. it is taking sizes of next frame and next frame and so on.
Now this UIImageView contains a subview which is also imageview. I will call it bSubView.
Now i want to resize this bSubView every time its parent's view frame changes.
Code is :- Here invisibleView is subview and viewHolder is parent view. parent view is changing right but problem is with subview.
UIImage *image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageString1]]];
UIImageView *viewHolder=[[UIImageView alloc] initWithFrame:CGRectMake(510, 200, 20, 20)];
invisibleView=[[UIImageView alloc] init];
invisibleView.image=image;
invisibleView.frame=viewHolder.bounds;
viewHolder.autoresizesSubviews=YES;
[ invisibleView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
invisibleView.contentMode=UIViewContentModeCenter;
viewHolder.image=[UIImage imageNamed:#"EmptyPlanet"];
[viewHolder addSubview:invisibleView];
[self.view addSubview:viewHolder];
Many Thanks in advance.
you can use the inherited method -(void)layoutsubviews
This method is called on the super view when its frame changes. In that method you can configure your subviews
Another option is to use autolayout when configuring the subviews.

Resources