how to change CropRect when it is loaded? - ios

I found a useful library which works with cropping image in this link. I want to use it with only one cropRect which is ratio = 3.0f / 4.0f; instead of choosing them. I added below code in viewDidApper method of PECropViewController:
CGFloat ratio = 3.0f / 4.0f;
CGRect cropRect = self.cropView.cropRect;
CGFloat width = CGRectGetWidth(cropRect);
cropRect.size = CGSizeMake(width, width * ratio);
self.cropView.cropRect = cropRect;
But it is not working. There is (void)constrain:(id)sender method which works when I press Edit button. If I write above code in this method it works well. Here is the code:
(void)constrain:(id)sender
{
CGFloat ratio = 3.0f / 4.0f;
CGRect cropRect = self.cropView.cropRect;
CGFloat width = CGRectGetWidth(cropRect);
cropRect.size = CGSizeMake(width, width * ratio);
self.cropView.cropRect = cropRect;
return;
{
The problem is I can't change cropRect's ratio when croppingView appears.

if somebody is looking for solution for this question, i found it. I have just changed cropAspectRatio in PECropView:
- (void)setupImageView
{
CGRect cropRect = AVMakeRectWithAspectRatioInsideRect(self.image.size, self.insetRect);
self.scrollView.frame = cropRect;
self.scrollView.contentSize = cropRect.size;
self.zoomingView = [[UIView alloc] initWithFrame:self.scrollView.bounds];
self.zoomingView.backgroundColor = [UIColor clearColor];
[self.scrollView addSubview:self.zoomingView];
self.imageView = [[UIImageView alloc] initWithFrame:self.zoomingView.bounds];
self.imageView.backgroundColor = [UIColor clearColor];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.imageView.image = self.image;
[self.zoomingView addSubview:self.imageView];
//i added this line code
self.cropAspectRatio = 4 / 3.0;
}

Related

UILabelView cropping when expanded, but renders fully when inspected in view hierarchy

Image 1 shows issue. label has flexible width & height constrains, also we adding fix width & height to expand with. in View hierarchy it shows fully expanded as expected, but issue in real view:
Image 2 shows fully expanded label in View hierarchy:
Lable code
self.textLabel = [[RAVerticallyAlignedLabel alloc] initWithFrame: self.bounds andVerticalTextAlignment: [self verticalTextAlignment]];
self.textLabel.backgroundColor = ClearColour;
self.textLabel.userInteractionEnabled = NO;
self.textLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.textLabel.numberOfLines = 0;
viewToTruncate = self.textLabel;
textWidth = MAX(MIN(screenSize.width - 20, [label.text sizeWithAttributes: #{NSFontAttributeName : label.font}].width + 10);
[UIView animateWithDuration: 0.3 animations: ^
{
self->containerView.frame = CGRectMake(xPos, top , MIN(textWidth, screenSize.width - 2 * RAPadding), newTextviewHeight);
// Container view is added inside scroll view
self->containerView.layer.shadowRadius = kRATruncationUtiltityShadowRadius;
self->containerView.layer.shadowOpacity = kRATruncationUtiltityShadowOpacity;
self->containerView.layer.shadowOffset = kRATruncationUtiltityShadowOffset;
self->containerView.layer.shadowPath = [UIBezierPath bezierPathWithRect: self->containerView.bounds].CGPath;
self->containerView.layer.masksToBounds = NO;
self->viewToExpand.frame = CGRectMake(0, 0, textWidth + componentContentWidth, self->containerView.height);
//viewToExpand is UILable, added inside container view
self->viewToExpand.layer.borderWidth = 1.0;
self->viewToExpand.layer.borderColor = borderColor.CGColor;
self->viewToExpand.layer.backgroundColor = backgroundColor.CGColor;
}];
Found issue fix, it size wasn't issue but "gradientMask" was playing up on expand as below:
CAGradientLayer *gradientMask = [CAGradientLayer layer];
gradientMask.frame = CGRectMake(0, 0, viewToTruncate.width, viewToTruncate.height);
gradientMask.colors = #[(id)WhiteColour.CGColor,(id)WhiteColour.CGColor, (id)ClearColour.CGColor];
gradientMask.locations = #[#0, #(locationOfFade), #1];
viewToTruncate.layer.mask = gradientMask;
removing mask on expand , it was rendering as expected.

iOS: Make image width slightly smaller than screen width

I'm having an issue where my image is too big. I need it to be slightly less than what the screen width is.
Here's my controller:
- (void)viewDidLoad {
[super viewDidLoad];
self.imageName = #"goldencoaster";
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"table"]]];
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:#"%#", self.imageName]];
CGRect bounds;
bounds.origin = CGPointZero;
bounds.size = [[UIScreen mainScreen] bounds].size;
self.coasterImage.bounds = bounds;
self.coasterImage.image = image;
}
However, this returns this when I run the simulator:
How would I get the image to show up slightly smaller than the screen width?
====================================================================
UPDATE
So I took out auto layout as suggested, and updated my controller to look like this:
- (void)viewDidLoad {
[super viewDidLoad];
self.imageName = #"goldencoaster";
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"table"]]];
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:#"%#", self.imageName]];
CGRect screen = [[UIScreen mainScreen] bounds];
CGRect bounds;
bounds.origin.x = screen.origin.x + 10;
bounds.origin.y = screen.origin.y + 10;
bounds.size.width = screen.size.width - 100;
self.coasterImage.frame = bounds;
self.coasterImage.image = image;
}
However, now my image doesn't show up at all?? Only the background (image with name "table") shows up, but not the goldencoaster.
Try this
- (void)viewDidLoad {
[super viewDidLoad];
self.imageName = #"goldencoaster";
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"table"]]];
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:#"%#", self.imageName]];
CGRect bounds;
bounds.origin = CGPointZero;
bounds.size = [[UIScreen mainScreen] bounds].size;
self.coasterImage.bounds = bounds;
self.coasterImage.image = image;
self.coasterImage.frame = CGRectMake(20, 0, self.view.frame.size.width-40, self.view.frame.size.height);
self.coasterImage.center = self.coasterImage.superview.center;
}
Swift 3.1
override func viewDidLoad() {
super.viewDidLoad()
imageName = "goldencoaster"
view.backgroundColor = UIColor(patternImage: UIImage(named: "table")!)
let image = UIImage(named: imageName)
var bounds = CGRect()
bounds.origin = CGPoint.zero
bounds.size = (UIScreen.main.bounds).size
coasterImage.bounds = bounds
coasterImage.image = image
coasterImage.frame = CGRect(x: CGFloat(20), y: 0, width: self.view.frame.size.width - CGFloat(40), height: self.view.frame.size.height)
coasterImage.center = (coasterImage.superview?.center)!
}
you can do it with contentMode like
self.coasterImage.contentMode = UIViewContentModeScaleAspectFit;
here are the different contentMode check it from here
UIViewContentModeScaleToFill
UIViewContentModeScaleAspectFit // contents scaled to fit with fixed aspect. remainder is transparent
UIViewContentModeScaleAspectFill // contents scaled to fill with fixed aspect. some portion of content may be clipped.
UIViewContentModeRedraw // redraw on bounds change (calls -setNeedsDisplay)
UIViewContentModeCenter // contents remain same size. positioned adjusted.
UIViewContentModeTop
UIViewContentModeBottom
UIViewContentModeLeft
UIViewContentModeRight
UIViewContentModeTopLeft
UIViewContentModeTopRight
UIViewContentModeBottomLeft
UIViewContentModeBottomRight
Two words: Auto Layout
You want your UI to adapt to different devices, and orientations.
Auto Layout and size classes are designed to handle those needs.
Update:
You're checking screen size and setting bounds. That suggests you're trying to size the view instead of letting Auto Layout constrain it. Auto Layout takes the place of setting frame or bounds. You don't want to mix the two.
If you weren't using autolayout, you will need to edit the image frame. For example if you want to reduce the size in 10 pixels:
- (void)viewDidLoad {
[super viewDidLoad];
self.imageName = #"goldencoaster";
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"table"]]];
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:#"%#", self.imageName]];
CGRect screen = [[UIScreen mainScreen] bounds];
CGRect bounds = CGRectMake(screen.origin.x+10,
screen.origin.y+10,
screen.size.width-20,
screen.size.height-20);
self.coasterImage.center = self.coasterImage.superview.center;
self.coasterImage.frame = bounds;
self.coasterImage.image = image;
}

How can i set image circular

I have done following code but it does not work. It shows me in circle but in crop image format.
I want to fit image to circle.
UIImage * defaultImage = [UIImage imageNamed:#"empty.png"];
self.myImageView = [[UIImageView alloc] initWithImage:defaultImage];
CGRect myFrame = CGRectMake(90.0f, 100.0f, self.myImageView.frame.size.width/1.5,self.myImageView.frame.size.height/1.5);
[self.myImageView setFrame:myFrame];
[self.myImageView setContentMode:UIViewContentModeScaleAspectFit];
self.myImageView.layer.masksToBounds = NO;
self.myImageView.layer.cornerRadius = self.myImageView.frame.size.width/2;
self.myImageView.layer.borderColor = [UIColor blackColor].CGColor;
self.myImageView.clipsToBounds=YES;
self.myImageView.layer.borderWidth = 2.0f;
[self.view addSubview:self.myImageView];
You can use this method
-(void)setRoundedView:(UIImageView *)roundedView toDiameter:
(float)newSize;
{
CGPoint saveCenter = roundedView.center;
CGRect newFrame = CGRectMake(roundedView.frame.origin.x,
roundedView.frame.origin.y, newSize, newSize);
roundedView.frame = newFrame;
roundedView.layer.cornerRadius = newSize / 2.0;
roundedView.center = saveCenter;
}
Now call the method with your imageview
[self setRoundedView:self.myImageView toDiameter:self.myImageView.frame.size.width];
self.myImageView.clipsToBounds = YES;
Hope it helps
In short,
anyView.layer.cornerRadius = anyView.frame.size.width/2;
Make sure that clipsToBound property of that view is enabled.
Set width & height equal to Your imageView, then set cornerRadius of imageView to half of width & height.
[self createRoundUIView:self.userProfileImageView sizeDiameter:self.userProfileImageView.frame.size.width];
And add following code to bellow method,
-(void)createRoundUIView:(UIImageView *)inputView sizeDiameter:(float)diameterSize
{
CGPoint saveCenter = inputView.center;
CGRect frame = CGRectMake(inputView.frame.origin.x, inputView.frame.origin.y, diameterSize, diameterSize);
inputView.frame = frame;
inputView.layer.cornerRadius = diameterSize / 2.0;
inputView.center = saveCenter;
inputView.clipsToBounds = YES;
}

UIScrollview calculation of new CGRect after ZoomToRect

My problem is i am not able to get the exact CGRect value after zooming for zooming in UIScrollview i am using zoomToRect method of UIScrollview. while zooming i am setting the contentOffset property.
Code follows:
if(image != nil) {
CGSize zoomViewSize = image.frame.size;
CGSize scrollViewSize = self.bounds.size;
if(zoomViewSize.width < scrollViewSize.width) {
anOffset.x = -(scrollViewSize.width - zoomViewSize.width) / 2.0;
}
if(zoomViewSize.height < scrollViewSize.height) {
anOffset.y = -(scrollViewSize.height - zoomViewSize.height) / 2.0;
}
}
super.contentOffset = anOffset;
after zooming i am setting the content inset property so that image comes in the center. for content inset the code is given below.
-(void)zoomtorect:(CGRect)rect animated:(BOOL)animated {
[super zoomtorect:rect animated:YES];
CGFloat pageWidth = image.image.size.height;
CGSize imageSize = rect.size;
CGSize zoomedImageSize = CGSizeMake(imageSize.width * 0.2, imageSize.height * 0.2);
CGSize pageSize = self.bounds.size;
//scLayer.frame = rect;
UIEdgeInsets inset = UIEdgeInsetsZero;
if (pageSize.width > zoomedImageSize.width) {
inset.left = (pageSize.width - zoomedImageSize.width) / 2;
inset.right = inset.left;
}
if (pageSize.height > zoomedImageSize.height) {
inset.top = (pageSize.height - zoomedImageSize.height) / 2;
inset.bottom = inset.top;
}
self.contentInset = inset;
}
this code shifts my image view between screen. Every thing works fine here. My problem is i have to show only zoom portion of the imageView rest will be black. For calculating the zoomed area i am doing the following calculation.
CGSize imageSize = rect.size;
CGSize zoomedImageSize = CGSizeMake(imageSize.width * scroll.maximumZoomScale, imageSize.height *scroll.maximumZoomScale);
CGSize pageSize = scroll.bounds.size;
UIEdgeInsets inset = UIEdgeInsetsZero;
if (pageSize.width > zoomedImageSize.width) {
inset.left = (pageSize.width - zoomedImageSize.width) / 2;
inset.right = inset.left;
}
if (pageSize.height > zoomedImageSize.height) {
inset.top = (pageSize.height - zoomedImageSize.height) / 2;
inset.bottom = inset.top;
}
CGRect zoomRect = [scroll convertRect:rect fromView:image];
CGRect zoomRect1 = CGRectMake(inset.left, inset.top+50, zoomRect.size.width, zoomRect.size.height);
this zoomRect1 is the zoomed area of the image this value is not exact what i am seeing in the screen. some where i am doing calculation mistake please help me out. But i am not getting the exact value of the zoomed image area.
Thanks in advance
Use this.. Works Perfectly
- (void)handleDoubleTap:(UIGestureRecognizer *)recognizer
{
if(isAlreadyZoomed)
{
CGPoint Pointview = [recognizer locationInView:recognizer.view];
CGFloat newZoomscal = 3.0;
CGSize scrollViewSize = self.scrollContainer.bounds.size;
CGFloat width = scrollViewSize.width/newZoomscal;
CGFloat height = scrollViewSize.height /newZoomscal;
CGFloat xPos = Pointview.x-(width/2.0);
CGFloat yPos = Pointview.y-(height/2.0);
CGRect rectTozoom=CGRectMake(xPos, yPos, width, height);
[self.scrollContainer zoomToRect:rectTozoom animated:YES];
[self.scrollContainer setZoomScale:3.0 animated:YES];
isAlreadyZoomed = NO;
}
else
{
[self.scrollContainer setZoomScale:1.0 animated:YES];
isAlreadyZoomed = YES;
}
}
isAlreadyZoomed is a BOOL value

iOS scroll view subviews

I have 3 scroll views which I added in IB then in my viewDidLoad I call a method that add a UIImageView to each subview. I would like to have zooming on each subview so I use
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return [scrollView.subviews objectAtIndex:0];
}
but viewForZoomingInScrollView is getting called before the imageviews are added to the subviews. How can solve this?
This is the loadImages method:
-(void) loadImages{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *imageData1 = [defaults dataForKey:#"image1"];
NSData *imageData2 = [defaults dataForKey:#"image2"];
NSData *imageData3 = [defaults dataForKey:#"image3"];
UIImage *img1 = [UIImage imageWithData:imageData1];
UIImage *img2 = [UIImage imageWithData:imageData2];
UIImage *img3 = [UIImage imageWithData:imageData3];
[self loadImage:img1 toScrollView:scrollViewTop];
[self loadImage:img2 toScrollView:scrollViewLeft];
[self loadImage:img3 toScrollView:scrollViewRight];
}
-(void) loadImage:(UIImage *)image toScrollView:(UIScrollView *)scrollView
{
//scale and rotate image to default
UIImage *scaledImage = scaleAndRotateImage(image);
//create imageview and add image to the view
UIImageView *imageView = [[UIImageView alloc]initWithImage:scaledImage];
CGFloat imageWidth = scaledImage.size.width;
CGFloat imageHeight = scaledImage.size.height;
int scrollWidth = scrollView.frame.size.width;
int scrollHeight = scrollView.frame.size.height;
//Limit by width or height, depending on which is smaller in relation to
//the scrollview dimension.
float scaleX = scrollWidth / imageWidth;
float scaleY = scrollHeight / imageHeight;
float scaleScroll = (scaleX < scaleY ? scaleY : scaleX);
// scrollView.bounds = CGRectMake(0, 0,imageWidth , imageHeight );
//scrollView.frame = CGRectMake(0, 0, scrollWidth, scrollHeight);
scrollView.delegate = self;
scrollView.contentSize = imageView.frame.size;
scrollView.pagingEnabled = NO;
scrollView.maximumZoomScale = scaleScroll*3;
scrollView.minimumZoomScale = scaleScroll;
scrollView.zoomScale = scaleScroll;
[scrollView addSubview:imageView];
}
Try adding the image view as a subview before you configure zooming. Say, before the delegate assignment.
viewForZoomingInScrollView: is getting called by one of the scroll view's setters; looking at the stack trace from there should tell you which one.

Resources