I am working on Pic Collage type of application. I have two scrollviews as shown in the figure below.
I have used this code to create an image to save in gallery
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultingImage;
But this creates an image of 320 X 460 resolution image which is not good for sharing or adding as a profile or cover photo on facebook/twitter etc.
This is why I tried to create everything programmatically when user clicks on Done. The only problem that I am facing is I am not able to get the same zoom scale here. When I am creating everything again programmatically, the scroll view should be zoomed so that when I combine the view for 640 X 960 resolution image, the scrollview subview i.e. image should also look like what user has set in the scrolls. Here is the code:
UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 640, 960)];
[mainView setBackgroundColor:[UIColor grayColor]];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 20, 290, 920)];
[scrollView setBackgroundColor:[UIColor redColor]];
[mainView addSubview:scrollView];
UIScrollView *scrollView11 = [[UIScrollView alloc] initWithFrame:CGRectMake(330, 20, 290, 920)];
[scrollView11 setBackgroundColor:[UIColor redColor]];
[mainView addSubview:scrollView11];
[scrollView1.layer setBorderWidth:0.0];
[scrollView2.layer setBorderWidth:0.0];
[[[scrollView1 subviews] objectAtIndex:0] removeFromSuperview];
[[[scrollView2 subviews] objectAtIndex:0] removeFromSuperview];
[scrollView1 setFrame:CGRectMake(20, 20, 290, 920)];
[scrollView2 setFrame:CGRectMake(330, 20, 290, 920)];
UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 290, 100)];
[imgV setImage:[self resizeImage:[UIImage imageNamed:#"photo1.png"] width:1166 height:960]];
//[imgV sizeToFit];
[scrollView1 addSubview:imgV];
UIImageView *imgV1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 290, 100)];
[imgV1 setImage:[self resizeImage:[UIImage imageNamed:#"photo2.png"] width:846 height:960]];
//[imgV1 sizeToFit];
[scrollView2 addSubview:imgV1];
scrollView1.contentSize = imgV.image.size;
scrollView2.contentSize = imgV1.image.size;
NSLog(#"scrollview subviews - %#",[scrollView1 subviews]);
[self initScrollView:scrollView1];
[self initScrollView:scrollView2];
[self centerScrollViewContents:scrollView1 withImageView:imgV];
[self centerScrollViewContents:scrollView2 withImageView:imgV1];
CGFloat newZoomScale = scrollView1.zoomScale;
// Figure out the rect we want to zoom to, then zoom to it
CGSize scrollViewSize = scrollView1.bounds.size;
CGFloat w = scrollViewSize.width / newZoomScale;
CGFloat h = scrollViewSize.height / newZoomScale;
CGFloat x = point.x - (w / 2.0f);
CGFloat y = point.y - (h / 2.0f);
CGRect rectToZoomTo = CGRectMake(rect.origin.x*2, rect.origin.y*2, rect.size.width*2, rect.size.height*2);
CGRect rectToZoom = CGRectMake(x, y, w, h);
[scrollView1 zoomToRect:rectToZoom animated:YES];
newZoomScale = scrollView2.zoomScale;
// Figure out the rect we want to zoom to, then zoom to it
scrollViewSize = scrollView2.bounds.size;
w = scrollViewSize.width / newZoomScale;
h = scrollViewSize.height / newZoomScale;
x = point1.x - (w / 2.0f);
y = point1.y - (h / 2.0f);
rectToZoomTo = CGRectMake(rect1.origin.x*2, rect1.origin.y*2, rect1.size.width*2, rect1.size.height*2);
rectToZoom = CGRectMake(x, y, w, h);
[scrollView2 zoomToRect:rectToZoom animated:YES];
[mainView addSubview:scrollView1];
[mainView addSubview:scrollView2];
[scrollView1.layer setBorderWidth:0.0];
[scrollView2.layer setBorderWidth:0.0];
Please help.
Thanks in Advance!
Related
I want to add some UILabels on top of my UIImagePickerController's custom OverlayView but can't seem to get it working.
Here is my code.
-(IBAction)getPhoto:(id)sender{
picker = [[UIImagePickerController alloc] init];
picker.allowsEditing = NO;
picker.sourceType = UIImagePickerControllerSourceTypeCamera ;
picker.showsCameraControls = NO;
self.picker.navigationBarHidden = YES;
self.picker.toolbarHidden = YES;
OverlayView = [[OverlayView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,1024)];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(OverlayView.frame.size.height/2, OverlayView.frame.size.width/2, 30, 100)];
label.textColor = [UIColor whiteColor];
label.text = #"find Me";
[self.OverlayView addSubview:label];
[self.OverlayView bringSubviewToFront:label];
picker.cameraOverlayView = OverlayView;
CGSize screenSize = [[UIScreen mainScreen] bounds].size; // 320 x 568
float scale = screenSize.height / screenSize.width*3/4; // screen height divided by the pickerController height ... or: 568 / ( 320*4/3 )
CGAffineTransform translate=CGAffineTransformMakeTranslation(0,(screenSize.height - screenSize.width*4/3)*0.5);
CGAffineTransform fullScreen=CGAffineTransformMakeScale(scale, scale);
picker.cameraViewTransform =CGAffineTransformConcat(fullScreen, translate);
[self presentViewController: picker animated:YES completion:NULL];
}
the overview shows up and it is full screen but no label
any ideas
Your label is just offscreen, check its frame
You can place it for instance like that, at least it will be visible
self.overlayView = [[OverlayView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(self.overlayView.frame.size.width/2, self.overlayView.frame.size.height/2, 30, 100)];
I a have UIView1 that hold a child UIView2,UIView2 hold a child UILabel.
Is it possible to get x,y of that UILabel depending onUIView1.
I tried to work with convertPoint without success.
Try this out!
CGRect NewFrame = [View1 convertRect:YourLabel.frame fromView:View2];
check your NewFrame according to first view.
CGFloat Xposition = NewFrame.frame.origin.x;
CGFloat Yposition = NewFrame.frame.origin.y;
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 400, 400)];
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 10, 10)];
[view1 addSubview:view2];
[view2 addSubview:label];
CGRect rect = [label.superview convertRect:label.frame toView:view1];
I am creating a UI that the user can page through multiple views by using a UIScrollView. I create the UIView objects in code and add them to UIScrollView. I use the code below to create the views.
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *brandView = [[UIView alloc] initWithFrame:frame];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 50.0, frame.size.width, 30)];
label.text = [brand objectForKey:#"name"];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor redColor];
[brandView addSubview:label];
UIImageView *logoImageView = [[UIImageView alloc] init];
logoImageView.frame = CGRectMake(20.0, 100.0, 280.0, 300.0);
[brandView addSubview:logoImageView];
logoImageView.file = [brand objectForKey:#"logo"];
[logoImageView loadInBackground];
Although I give 50.0 for the y coordinate for the label, I can't see the label at all when I run the app. And logoImageView has 100.0 for its top but it appears just below the status bar, as if it had 0.0 for it's y coordinate.
What is the reason for this strange behaviour, am I doing something wrong?
Note: Auto layout for the UIScrollView is disabled on IB.
Below code will work for you !
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * 10;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *brandView = [[UIView alloc] init]; // CHANGE THAT HAS TO BE DONE
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 50.0, self.scrollView.frame.size.width, 30)];
label.text = #"Test";
label.textAlignment = UITextAlignmentCenter; // DEPRECATED IN iOS 6.0
label.backgroundColor = [UIColor redColor];
[brandView addSubview:label];
[self.scrollView addSubview:brandView];
I used the below codes
CGRect scrollserviceViewFrame1 = CGRectMake(60,610, 650, 150);
//CGRect scrollViewFrame =[[UIScreen mainScreen] applicationFrame];
//UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
UIScrollView *servicescrollView1 = [[UIScrollView alloc] initWithFrame:scrollserviceViewFrame1];
[self.view addSubview:servicescrollView1];
//scrollView.backgroundColor=[UIColor greenColor];
[servicescrollView1 setBackgroundColor:[UIColor redColor]];
[servicescrollView1 setCanCancelContentTouches:NO];
//scrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
servicescrollView1.clipsToBounds = YES; // default is NO, we want to restrict drawing within our scrollview
servicescrollView1.scrollEnabled = YES;
servicescrollView1.autoresizesSubviews=YES;
[servicescrollView1 setContentSize: CGSizeMake(1050, 100)];
//scrollView.delegate = self;
servicescrollView1.pagingEnabled = YES;
servicescrollView1.showsHorizontalScrollIndicator = NO;
[servicescrollView1 scrollRectToVisible:CGRectMake(servicescrollView1.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height) animated:YES];
NSLog(#" scroll view width :%i ",servicescrollView1.frame.size.width);
NSLog(#"scroll view height :%i ",self.view.frame.size.height);
NSLog(#"self view width :%i",self.view.frame.size.width);
CGRect CGRectZero = CGRectMake(60,710,200, 100);
UIView* myserviceView1 = [[UIView alloc] initWithFrame: CGRectZero];
myserviceView1.backgroundColor=[UIColor blackColor];
[servicescrollView1 addSubview:myserviceView1];
[myserviceView1 release];
[servicescrollView1 release];
The scroll view is created , butcannot add the UIVIew to scrollview...
Any suggestions for solving this query???
Thanks in advance
Near the bottom of your code where you create myserviceView1, you (attempt to) reset a constant (CGRectZero).
And then when you create your myserviceView1 UIView, you're giving it a frame size of {0, 0, 0, 0}.
Ok, so i got a scrollview to load when an user logs in to my application. When the user is authenticated, i need to create a scrollview programmatically. To do so, i have the following code:
[scrollView removeFromSuperview];
UIScrollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, applicationFrame.size.width, self.view.frame.size.height)];
view.backgroundColor = [UIColor greenColor];
//-- get the y-coordinate of the view
CGFloat viewCenterY = self.view.center.y;
//--calculate how much visible space is left
CGFloat freeSpaceHeight = applicationFrame.size.height - keyboardBounds.size.height;
//-- calculate how much the scrollview must scroll
CGFloat scrollAmount = viewCenterY - freeSpaceHeight / 2.0;
if(scrollAmount < 0) scrollAmount = 0;
//set the new scrollsize contentview
view.contentSize = CGSizeMake(applicationFrame.size.width, applicationFrame.size.height + keyboardBounds.size.height);
//scroll the scrollview
[view setContentOffset:CGPointMake(0, scrollAmount) animated:YES];
for(int v = 0; v < 15; v++){
CGFloat yCoord = v * 100;
UILabel *mijnLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, yCoord, 200, 50)];
mijnLabel.text = #"1337";
mijnLabel.font = [UIFont fontWithName:#"Verdana" size:20];
mijnLabel.textColor = [UIColor redColor];
[view addSubview:mijnLabel];
[mijnLabel release];
}
[self.view addSubview:view];
[view release];
}
How do i extend my view according to the amount of labels i added? I also want to know how i can make my scrollview fill up the entire screen.. My scrollview is now only filling up the screen when in portrait mode.
This is the login screen that gets removed:
[scrollView removeFromSuperview];
you need to keep track of numbers of label that you are adding into the scroll view. suppose if your label's height is 50 then take a variable and make a total of all label's height and give a new frame to your scroll view. like :
view.frame = CGRectmake(0,0,applicationFrame.size.width,totalHeightOfLabel);
[self.view addSubview:view];
[view release];
hope this will give a idea...