image display in uiscrollview - iphone

Am facing a probem with my scrollview.Am displaying the image in a scrollview programatically and i added the zooming and paning effects.When i run the application image is displaying in the scroll view but it is not in centre(like it is displaying at right corner and some part of image is inside the scrollview)After zooming when i scroll the image the image is moving and i can see the black area which i dont want.
Please,somebody can help me
Thanks in advance
here is the code
mainView2 = [[UIScrollView alloc]init];
[mainView2 setFrame:CGRectMake(imageView2.frame.origin.x, imageView2.frame.origin.y, 222, 120)];
[mainView2 setContentSize:CGSizeMake(600,600)];
[mainView2 setMaximumZoomScale:2.0];
[mainView2 addSubview:imageView2];
mainView2.delegate = self;
[self.view addSubview:mainView2];
mainView2.scrollsToTop = NO;
here mainview2 is my scrollview and imageview2 is the uiimageview which i want to display in scrollview

I don't know exactly what you mean "I can see the black Area". However, the "center image" problem is quite a common one. As far as I know, there is no out of the box way to solve this. Fortunately, there is some great code on github which handels image scrolling in a UIScrollView nicely. See this question and this github project for further information.

Related

Dilemma with buttons within UIScrollView

I've got a UIScrollView (scrollView) and a UIImageView (scrollImage). I'm trying to add buttons to a scroll view and have them resize and reposition correctly when I zoom in and out. I had the following code at first to add a button:
[scrollView addSubview:button];
This gave me problems with zooming in and out. The position of the buttons wouldn't stay relative to the image. So i changed it to this:
[scrollImage addSubview:button];
Which made everything zoom in and out nicely, however the buttons have stopped reacting to touches (I assume because they are beneath the UIScrollView).
Is there a way to have both features working correctly?
Thanks
Zac
By default a UIImageView has userInteractionEnabled set to NO. Set it to YES and you should be fine.
try by setting like this,
scrollImage.userInteractionEnabled=YES

How do I resize a picture using UIImageView so that I can zoom in and out?

In my view I have a picture.. The picture has a 3:2 scale and is huge, resolution wise.
I'm having a lot of trouble initializing a UIImage with the image to the original size and then zooming out of the UIImageView so that the entire image is visible within the scrollview.
The program stays only in the portrait orientation.
And no, please don't say to just use UIWebView. UIWebView doesn't let me set the content size during view load...instead, it just zooms out of the image by some arbitrary scale and I couldn't figure out a way to adjust the scale value (I don't think it's possible).
Thanks for any responses! I really appreciate them :D
Here's an example of placing an image that responds to pinch-to-zoom. Basically, you place the UIImageView in a UIScrollView and change some settings.
UIImageView *myImage;
UIScrollView *myScroll;
-(void)viewDidLoad{
myImage = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,360,450)];
[myImage setImage:[UIImage imageNamed:#"coolpic.png"]];
myImage.contentMode = UIViewContentModeScaleAspectFit;
[myScroll addSubview:myImage];
[myScroll setContentSize:CGSizeMake(myImage.frame.size.width, myImage.frame.size.height)];
[myScroll setMinimumZoomScale:1.0];
[myScroll setMaximumZoomScale:4.0];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return myImage;
}
Of course, set all your delegates and IB hooks properly.
Edit: I just reread your question. The portion of the example that answers your question is the frame specification of the UIImageView and its contentMode setting.
You almost certainly don't want to load your 'huge, resolution wise' image all at once on load regardless of it's scale. I'd suggest checking out some of Apple's sample code on this stuff (starting with ScrollViewSuite would be good, I'd say).
There was also a recent video released from WWDC where they implement this sort of thing live (they have more of a photo viewing app, but you could pretty easily do what they show with just one image too) so take a look for that too.
Go to the attributes tab of the UIImageView. Select "aspect to fill" or "aspect to fit". If you use "aspect to fill", you might not show the whole image. I just use "aspect to fit", and make the background black.

How to scroll the content without scrolling the image in the background using UIScrollView?

I am trying to scroll the content without scrolling the image in the background using UIScrollView.
I am using IB and setting the FileOwner View to point to the Scroll View (Image View is a child of the Scroll view). I have made the image height to be 960 pixels.
I have also set scrolling content size in the vierController that owns this UIView
(void)viewDidLoad {
UIScrollView *tempScrollView = (UIScrollView *)self.view;
tempScrollView.contentSize=CGSizeMake(320, 960);
}
My problem is that the Image only appears moves along with the content.
I have tried taking out the settings in viewDidLoad, but the scrolling cease to function.
I have also tried changing the location of the image and have it placed under VIEW instead of Scoll View (by the way Scroll View is a child of VIEW), but that resulted in the app breaking (termination error).
Any advice would be appreciated.
The easiest (and correct way) is to set an background image to your UIScrollView
UIImage *img = [UIImage imageNamed:#"image.png"];
[scrollView setBackgroundColor:[UIColor colorWithPatternImage:img]];
The easiest way is to set an UIImageView behind your UIScrollView(UIImageView and UIScrollView under UIView) and make your UIScrollView background color as clear color as mentioned by Janson. By this way you can scroll the content without scrolling the image in the background using UIScrollView.
Thanks. This was extremely helpful.
The one thing that I would add (pardon if it is obvious, but for people new to the platform (i.e., me), it took a bit of playing around).
I put the code from "unset"'s example after where I setup the contentSize=CGSizeMake in the viewDidLoad of the UIViewController , i.e.,:
// Setup the Scroll View
UIScrollView *tempScrollView=(UIScrollView *)self.view;
tempScrollView.contentSize=CGSizeMake(320, 720);
// Set Stationary Background, so that while the user scroll the background is
// fixed.
UIImage *img = [UIImage imageNamed:#"bg-body.jpg"];
[tempScrollView setBackgroundColor:[UIColor colorWithPatternImage:img]];
Then, I went into the ViewController .xib file and on the UIView that is inside of the UIScrollView, I set the background color to "Clear Color".
I sure that there is a way to do this through code at the same time as setting the background of the UIScrollView, but at least that is how you can do it through IB.
Jason

Combining images

I want to place the picked image on top of another image, so that my picked image will be placed in some sort of frame I made in Photoshop. After combining the images I want to save it to the disk.
Does anyone know how to do it, or maybe have a link to examples?
You can just layer the two images on top of each other, first add the frame image, then add the image with the photo...
Heres sample code
Assuming your 2 images are already sized correctly to fit one on top of t he other, this code would be in a view controller
UIImageView *frame=[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Frame.png"]];
UIImageView *pic=[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"pic.png"]];
frame.center=[self.view center];
pic.center=[self.view center];
[self.view addSubview:frame];
[self.view addSubview:pic];
here it is, memory managment has not been written in..
You can also add the picture UIImageView directly the the frame UIImageView: same as Daniel's suggestion above, but [frame.view addSubview: pic] instead.

Gray out view in iphone, how?

I wonder what is the way to gray out portion of a view similar to the way UIAlertView grays out everything except the message box? Right now i use another custom view on top of the target area, but it doesnt look as nice.
Any ideas?
I get good results using the method you have already tried. perhaps fiddling around with the alpha is a good idea?
mask = [[UIView alloc] initWithFrame:window.frame];
[mask setBackgroundColor:[UIColor colorWithWhite:0.0 alpha:0.78]];
[self.view addSubview:mask];
Then later in your code you can remove it:
[mask removeFromSuperview];
or
[mask setHidden:YES];
If you want to make it even better, I suppose you could try using a gradient, either programmatically, or as an image, and using this to darken the edges of the screen such that the content you are displaying forefront appears to be the light source.