I have a scrollview with subview as images.. when I made the pictures center to the scrollview they all appear on top of each other as a stack.. I need them to scroll horizontally.. how do I fix that? this is my code so far..
- (void)viewDidLoad
{
[super viewDidLoad];
for (int i = 1; i < 18; i++) {
UIImageView *images = [[UIImageView alloc] initWithImage:[UIImage imageNamed: [NSString stringWithFormat:#"%d.jpg", i]]];
images.frame = CGRectMake((i-1)*320, 0, 320, 330);
[scroller setContentOffset:CGPointMake(0, 0)];
[scroller addSubview:images];
[images setContentMode:UIViewContentModeScaleAspectFill];
[images sizeToFit];
//center image
CGSize boundsSize = scroller.bounds.size;
CGRect frameToCenter = images.frame;
// center horizontally
if (frameToCenter.size.width < boundsSize.width) {
frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
NSLog(#"%f", frameToCenter.origin.x);
}
else
frameToCenter.origin.x = 0;
// center vertically
if (frameToCenter.size.height < boundsSize.height)
frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
else
frameToCenter.origin.y = 0;
images.frame = frameToCenter;
scroller.pagingEnabled = NO;
}
[scroller setContentMode:UIViewContentModeScaleAspectFit];
scroller.delegate =self;
scroller.contentSize = CGSizeMake(320*17, 330);
scroller.backgroundColor = [UIColor blackColor];
pageControl.numberOfPages =17;
pageControl.currentPage = 0;
To center the images horizontally just use the code below
float scrollHeight = 0;
for (int col = 0; col < index; ++col)
{
CGRect selectFrame = CGRectMake(0,0,kOvrW,kOvrH);
selectionIndicator = [[UIImageView alloc] initWithFrame:selectFrame];
selectionIndicator.contentMode = UIViewContentModeBottomRight;
[selectionIndicator setImage:[UIImage imageNamed:#"checked"]];
CGRect frame = CGRectMake(5,kOvrPreY+col*(kOvrPreH+kOvrH),kOvrW,kOvrH);
UIView *fr = [[UIView alloc] initWithFrame:frame];
CGRect imgFrame = CGRectMake(5, 4, 68, 55);
UIImageView *imgView = [[UIImageView alloc]initWithFrame:imgFrame];
imgView.image = [UIImage imageNamed:[[subCategories objectAtIndex:col] valueForKey:#"add"]];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tapAction:)];
tapGesture.numberOfTapsRequired = 1;
tapGesture.numberOfTouchesRequired = 1;
[fr setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"small-thumb"]]];
[fr addGestureRecognizer:tapGesture];
[fr addSubview:imgView];
fr.tag = col;
[subCatScroll addSubview:fr];
[subCatScroll bringSubviewToFront:fr];
scrollHeight += kOvrPreH+kOvrH;
}
subCatScroll.contentSize = CGSizeMake(subCatScroll.frame.size.width, scrollHeight+kOvrPreH);
Related
I am using the ios default camera in my application. I would like to change something the edit view that shows after the user takes a photo.Normally, it shows a rectangle to crop, but I would like it to show a circle how would I do this.
Here is the solution which might help you to create crop overlay:-
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if ([navigationController.viewControllers count] == 3)
{
CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
UIView *plCropOverlay = [[[viewController.view.subviews objectAtIndex:1]subviews] objectAtIndex:0];
plCropOverlay.hidden = YES;
int position = 0;
if (screenHeight == 568)
{
position = 124;
}
else
{
position = 80;
}
CAShapeLayer *circleLayer = [CAShapeLayer layer];
UIBezierPath *path2 = [UIBezierPath bezierPathWithOvalInRect:
CGRectMake(0.0f, position, 320.0f, 320.0f)];
[path2 setUsesEvenOddFillRule:YES];
[circleLayer setPath:[path2 CGPath]];
[circleLayer setFillColor:[[UIColor clearColor] CGColor]];
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 320, screenHeight-72) cornerRadius:0];
[path appendPath:path2];
[path setUsesEvenOddFillRule:YES];
CAShapeLayer *fillLayer = [CAShapeLayer layer];
fillLayer.path = path.CGPath;
fillLayer.fillRule = kCAFillRuleEvenOdd;
fillLayer.fillColor = [UIColor blackColor].CGColor;
fillLayer.opacity = 0.8;
[viewController.view.layer addSublayer:fillLayer];
UILabel *moveLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, 320, 50)];
[moveLabel setText:#"Move and Scale"];
[moveLabel setTextAlignment:NSTextAlignmentCenter];
[moveLabel setTextColor:[UIColor whiteColor]];
[viewController.view addSubview:moveLabel];
}
}
In one of my application I need to split UIImage into multiple parts. The following was the code I am using to split. Here my problem is I am unable to load the image view by adding the image to UIImageView.
- (void)viewDidLoad
{
UIImage* image = [UIImage imageNamed:#"monalisa.png"];
NSMutableArray* splitImages = [self splitImageIntoRects:(__bridge CGImageRef)(image)];
printf("\n count; %d",[splitImages count]);
CALayer *layer = [splitImages objectAtIndex:5];
CGImageRef imgRef = (__bridge CGImageRef)(layer.contents);
UIImage *img = [[UIImage alloc] initWithCGImage:imgRef];
UIImageView* imageview = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
imageview.image = img;
imageview.backgroundColor = [UIColor redColor];
[self.view addSubview:imageview];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (NSMutableArray*)splitImageIntoRects:(CGImageRef)anImage
{
CGSize imageSize = CGSizeMake(CGImageGetWidth(anImage), CGImageGetHeight(anImage));
NSMutableArray *splitLayers = [NSMutableArray array];
int kXSlices = 3;
int kYSlices = 3;
for(int x = 0;x < kXSlices;x++) {
for(int y = 0;y < kYSlices;y++) {
CGRect frame = CGRectMake((imageSize.width / kXSlices) * x,
(imageSize.height / kYSlices) * y,
(imageSize.width / kXSlices),
(imageSize.height / kYSlices));
CALayer *layer = [CALayer layer];
layer.frame = frame;
CGImageRef subimage = CGImageCreateWithImageInRect(anImage, frame);
layer.contents = (__bridge id)subimage;
[splitLayers addObject:layer];
}
}
return splitLayers;
}
And the output is like follows,
Try This:
- (void)viewDidLoad
{
[super viewDidLoad];
[self getSplitImagesFromImage:[UIImage imageNamed:#"Image1.png"] withRow:4 withColumn:4];
}
-(NSMutableArray *)getSplitImagesFromImage:(UIImage *)image withRow:(NSInteger)rows withColumn:(NSInteger)columns
{
NSMutableArray *aMutArrImages = [NSMutableArray array];
CGSize imageSize = image.size;
CGFloat xPos = 0.0, yPos = 0.0;
CGFloat width = imageSize.width/rows;
CGFloat height = imageSize.height/columns;
for (int aIntY = 0; aIntY < columns; aIntY++)
{
xPos = 0.0;
for (int aIntX = 0; aIntX < rows; aIntX++)
{
CGRect rect = CGRectMake(xPos, yPos, width, height);
CGImageRef cImage = CGImageCreateWithImageInRect([image CGImage], rect);
UIImage *aImgRef = [[UIImage alloc] initWithCGImage:cImage];
UIImageView *aImgView = [[UIImageView alloc] initWithFrame:CGRectMake(aIntX*width, aIntY*height, width, height)];
[aImgView setImage:aImgRef];
[aImgView.layer setBorderColor:[[UIColor blackColor] CGColor]];
[aImgView.layer setBorderWidth:1.0];
[self.view addSubview:aImgView];
[aMutArrImages addObject:aImgRef];
xPos += width;
}
yPos += height;
}
return aMutArrImages;
}
for more info see this and you can also download demo from here.
We can enhance more the Yasika Patel Answer. Below function will give you exact peice of image which fits to your view.
- (void)splitImage :(UIImage *)image withColums:(int)columns WithRows: (int)rows ViewToIntegrate : (UIView *)view
{
CGSize imageSize = _imgSplit.image.size;
CGFloat xPos = 0.0, yPos = 0.0;
CGFloat width = imageSize.width/rows;
CGFloat height = imageSize.height/columns;
for (int aIntY = 0; aIntY < columns; aIntY++)
{
xPos = 0.0;
for (int aIntX = 0; aIntX < rows; aIntX++)
{
CGRect rect = CGRectMake(xPos, yPos, width, height);
CGImageRef cImage = CGImageCreateWithImageInRect([ image CGImage], rect);
UIImage *aImgRef = [[UIImage alloc] initWithCGImage:cImage];
UIImageView *aImgView = [[UIImageView alloc] initWithFrame:CGRectMake(aIntX*(view.frame.size.width/rows), aIntY*( view.frame.size.height/columns), view.frame.size.width/rows, view.frame.size.height/columns)];
[aImgView setImage:aImgRef];
[aImgView.layer setBorderColor:[[UIColor blackColor] CGColor]];
[aImgView.layer setBorderWidth:0.5];
[view addSubview:aImgView];
xPos += width;
}
yPos += height;
}
[self.view addSubview:view];
}
This will give you the image in 9parts . here you just need to pass the row and colums.
I currently have a scroll view loading images from an array, here's my code:
icons = [[NSMutableArray alloc]initWithObjects:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"one" ofType:#"png"]], [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"two" ofType:#"png"]], [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"three" ofType:#"png"]],nil];
int numberOfItems = [icons count];
NSLog(#"Number of objects: %i",numberOfItems);
for (int i = 0; i < numberOfItems; i++) {
if (i <= 3) {
finalFrame = CGRectMake( i*(80), 12, 80, 80);
}
UIButton *iconButton = [UIButton buttonWithType:UIButtonTypeCustom];
iconButton = [UIButton buttonWithType:UIButtonTypeCustom];
iconButton.frame = CGRectMake(0, 0, 57, 57);
iconButton.center = CGPointMake(40, 29);
iconButton.imageView.contentMode = UIViewContentModeScaleToFill;
[iconButton addTarget:self action:#selector(selected:) forControlEvents:UIControlEventTouchUpInside];
iconButton.tag = i;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
UIImage *icon = [icons objectAtIndex:i];
dispatch_sync(dispatch_get_main_queue(), ^{
[iconButton setImage:icon forState:UIControlStateNormal];
});
});
[sub addSubview:iconButton];
sub = [[UIView alloc] initWithFrame:finalFrame];
[scrollView addSubview:sub];
}
The NSLog reads 3 objects, but my scroll view is only showing 2 of them, what am I doing wrong?
I think in it is in for loop for it should be like this
(int i = 0; i <= numberOfItems; i++)
I want to animate a UIImageView left to right with UIPageControl. I have this code but it seems a bit odd...Specifically because what would I do when I reach image 3? I would have to return and that would mess up the mathematical calculations of the views in the pageControl array the way I have it set up:
-(void)createUIViews{
UIImage *image1 = [UIImage imageNamed:#"GiftCard.png"];
firstView = [[UIImageView alloc] initWithImage:image1];
firstView.backgroundColor = [UIColor grayColor];
firstView.tag = 1;
firstView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);
[self.view addSubview:firstView];
CGRect newFrame = firstView.frame;
newFrame.origin.x += 40; // shift right by 50pts
newFrame.origin.y += 40;
[UIView animateWithDuration:1.0
animations:^{
firstView.frame = newFrame;
}];
UIImage *image2 = [UIImage imageNamed:#"MyCard.png"];
secondView = [[UIImageView alloc] initWithImage:image2];
secondView.backgroundColor = [UIColor grayColor];
secondView.tag = 1;
secondView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);
UIImage *image3 = [UIImage imageNamed:#"GiftCard.png"];
thirdView = [[UIImageView alloc] initWithImage:image3];
thirdView.backgroundColor = [UIColor grayColor];
thirdView.tag = 1;
thirdView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self createUIViews];
// Set up the views array with 3 UIImageViews
views = [[NSArray alloc] initWithObjects:firstView, secondView, thirdView, nil];
// Set pageControl's numberOfPages to 3
self.pageControl.numberOfPages = [views count];
// Set pageControl's currentPage to 0
self.pageControl.currentPage = 0;
// Call changePage each time value of pageControl changes
[self.pageControl addTarget:self action:#selector(changePage:) forControlEvents:UIControlEventValueChanged];
}
- (IBAction) changePage:(id)sender {
NSLog(#"pageControl position %d", [self.pageControl currentPage]);
int oldPageControlPosition = [self.pageControl currentPage] - 1;
NSLog(#"old pageControl position %d", oldPageControlPosition);
//0 Get the currentview as referenced by pageControl
UIImageView *oldView = [views objectAtIndex:oldPageControlPosition];
//NSLog(#"views objectAtIndex = %#",[views objectAtIndex:[self.pageControl currentPage]]);
//1 Animate out the old view
CGRect oldViewFrame = oldView.frame;
oldViewFrame.origin.x -= 300;
[UIView animateWithDuration:2.0
delay: 0.5
options: UIViewAnimationOptionCurveEaseIn
animations:^{
oldView.frame = oldViewFrame;
}
completion:nil];
//3 Get next view from array
UIImageView *nextView = [views objectAtIndex:[self.pageControl currentPage]];
//4 Add it to mainview
[self.view addSubview:nextView];
//5 Animate in
CGRect nextViewFrame = nextView.frame;
nextViewFrame.origin.x += 40; // shift right by 50pts
nextViewFrame.origin.y += 40;
[UIView animateWithDuration:1.0
animations:^{
nextView.frame = nextViewFrame;
}];
}
When I reach the end, I try to return, the last position is 2 and oldPosition at that time is 1. Which is correct, array goes from 0 to 1 to 2 for a total of 3 positions. But when I return, the log displays position as 1 instead of 2, which is fine, because I reversed...but old position shows 0 instead of 2.
I couldnt get it to determine direction so I ended up using gesture recognizers with swipe left and swipe right instances. Here is the code:
//Help determine direction of swipe
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeNext:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[self.scrollView addGestureRecognizer:swipeLeft];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipePrev:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[self.scrollView addGestureRecognizer:swipeRight];
i have an array for my UITextField to be created. but the UITextField remain on the same Frame?Here is my code. It create a new UITextField when you press the button.
-(IBAction)textFieldcreating{
textfieldform = [[NSMutableArray alloc] init];
textField1 = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 100, 40)];
textField1.borderStyle = UITextBorderStyleRoundedRect;
textField1.font = [UIFont systemFontOfSize:15];
textField1.placeholder = #"enter text";
textField1.autocorrectionType = UITextAutocorrectionTypeNo;
textField1.keyboardType = UIKeyboardTypeDefault;
textField1.returnKeyType = UIReturnKeyDone;
textField1.clearButtonMode = UITextFieldViewModeWhileEditing;
textField1.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField1.delegate = self;
[textfieldform addObject:textField1];
int counter;
counter = 0;
if(counter < [textfieldform count])
{
textField1.frame = CGRectMake(textField1.frame.origin.x+30, textField1.frame.origin.x+40, textField1.frame.size.width+40, textField1.frame.size.height+50);
[self.view addSubview:textField1];
counter++;
}
}
What you are doing is creating a new Array and keeping the xOrigin and yOrigin same for all the UITextViews!
Do something like this.
In .h file, declare
#property (nonatomic, retain) NSMutableArray *textfieldform;
#property (nonatomic, readwrite) int yOrigin;
in .m file, in viewDidLoad function,
self.textfieldform = [NSMutableArray arrayWithCapacity:0];
yOrigin = 0;
now, your function looks like this.
-(IBAction)textFieldcreating{
//textfieldform = [[NSMutableArray alloc] init]; NO NEED OF THIS LINE NOW
textField1 = [[UITextField alloc] initWithFrame:CGRectMake(10, yOrigin, 100, 40)];
textField1.borderStyle = UITextBorderStyleRoundedRect;
textField1.font = [UIFont systemFontOfSize:15];
textField1.placeholder = #"enter text";
textField1.autocorrectionType = UITextAutocorrectionTypeNo;
textField1.keyboardType = UIKeyboardTypeDefault;
textField1.returnKeyType = UIReturnKeyDone;
textField1.clearButtonMode = UITextFieldViewModeWhileEditing;
textField1.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField1.delegate = self;
[textfieldform addObject:textField1];
yOrigin = yOrigin + 40 + 10; //old yorigin + btn height + y offset
}
If you want changes in xOrigin too, then take another variable with xOrigin