How can i set image circular - ios

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;
}

Related

Scale image based off UIPinch

I am so confused with how to do what I need! Any help is very appreciated! I have an transparent image overlay that has another image behind it that has a UIPanGestureRecognizer and a UIPinchGestureRecognizer attached to it. The top image has a completely transparent middle that serves as the "glass" view. I am trying to crop the bottom image to the "glass" part based off pan and pinch. (see the image below to see what i'm talking about) I have successfully taken care of the pan crop but am having issues cropping it correctly when pinch is also applied.
I am not using snapshotViewAfterScreenUpdates.
Here is the code I have so far:
UIImage *snapshotImage;
/* draw the image of all the views below our view */
UIGraphicsBeginImageContextWithOptions(self.pendantImageView.bounds.size, NO, 0);
BOOL successfulDrawHierarchy = [self.pendantImageView drawViewHierarchyInRect:self.pendantImageView.bounds afterScreenUpdates:YES];
if ( successfulDrawHierarchy ) {
snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
} else {
NSLog(#"drawViewHierarchyInRect:afterScreenUpdates: failed - there's nothing to draw...");
}
UIGraphicsEndImageContext();
UIImage *croppedImage;
if ( successfulDrawHierarchy ) {
/* calculate the coordinates of the rectangle we're interested in within the returned image */
CGRect cropRect = CGRectOffset(pendantFrame, - self.pendantImageView.frame.origin.x, - self.pendantImageView.frame.origin.y);
/* draw the cropped section with a clipping region */
UIGraphicsBeginImageContextWithOptions(cropRect.size, YES, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClipToRect(context, CGRectMake(0, 0, cropRect.size.width, cropRect.size.height));
CGRect targetRectangeForCrop = CGRectMake(-cropRect.origin.x, -cropRect.origin.y, snapshotImage.size.width, snapshotImage.size.height);
[snapshotImage drawInRect:targetRectangeForCrop];
croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
pendantImageView is the bottom imageView and pendantFrame is the middle coords of the area i'm trying to crop to.
Thanks in advance!
I am not sure whether I understood you correctly, but here is my result:
(click on the video to watch full version)
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
UIImageView *theImageView = [UIImageView new];
[self.view addSubview:theImageView];
theImageView.image = [UIImage imageNamed:#"image1.jpg"];
theImageView.frame = self.view.frame;
theImageView.userInteractionEnabled = YES;
theImageView.alpha = 0.6;
UIImageView *thePanImageView = [UIImageView new];
[self.view addSubview:thePanImageView];
thePanImageView.frame = CGRectMake(0, 0, 100, 100);
thePanImageView.center = CGPointMake(theImageView.frame.size.width/2, theImageView.frame.size.height/2);
thePanImageView.image = [self screenshotFromRect:thePanImageView.frame fromView:theImageView];
thePanImageView.userInteractionEnabled = YES;
thePanImageView.layer.cornerRadius = thePanImageView.frame.size.width/2;
thePanImageView.clipsToBounds = YES;
thePanImageView.theWeakObject = theImageView;
{
UIPanGestureRecognizer *thePanGesture = [UIPanGestureRecognizer new];
[thePanGesture addTarget:self action:#selector(handlePanGesture:)];
[thePanImageView addGestureRecognizer:thePanGesture];
UIPinchGestureRecognizer *thePinchGesture = [UIPinchGestureRecognizer new];
[thePinchGesture addTarget:self action:#selector(handlePinchGesture:)];
[thePanImageView addGestureRecognizer:thePinchGesture];
}
}
- (void)handlePanGesture:(UIPanGestureRecognizer *)thePanGesture
{
UIImageView *thePanImageView = (id)thePanGesture.view;
UIImageView *theImageView = thePanImageView.theWeakObject;
thePanImageView.center = [thePanGesture locationInView:theImageView];
thePanImageView.image = [self screenshotFromRect:thePanImageView.frame fromView:theImageView];
}
- (void)handlePinchGesture:(UIPinchGestureRecognizer *)thePinchGesture
{
UIImageView *thePanImageView = (id)thePinchGesture.view;
static CGRect theInitialFrame;
if (thePinchGesture.state == UIGestureRecognizerStateBegan)
{
theInitialFrame = thePanImageView.frame;
}
else
{
CGRect theFrame = theInitialFrame;
theFrame.size.width *= thePinchGesture.scale;
theFrame.size.height *= thePinchGesture.scale;
thePanImageView.frame = theFrame;
}
thePanImageView.layer.cornerRadius = thePanImageView.frame.size.width/2;
UIImageView *theImageView = thePanImageView.theWeakObject;
thePanImageView.center = [thePinchGesture locationInView:theImageView];
thePanImageView.image = [self screenshotFromRect:thePanImageView.frame fromView:theImageView];
}
- (UIImage * __nonnull)screenshotFromRect:(CGRect)theRect fromView:(UIView * __nonnull)theView;
{
if (!theView)
{
abort();
}
if (theRect.size.height < 1 || theRect.size.width < 1)
{
abort();
}
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)])
{
UIGraphicsBeginImageContextWithOptions(theRect.size, YES, [UIScreen mainScreen].scale);
}
else
{
UIGraphicsBeginImageContext(theRect.size);
}
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ctx, -theRect.origin.x, -theRect.origin.y);
[theView.layer renderInContext:ctx];
UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return snapshotImage;
}
EDIT:
The above solution is inefficient in terms of CPU, because it takes screenshots every time you move the view.
A much more efficient way would be to create an extra UIImageView, and simply move it inside of your thePanImageView
The code is the following:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
UIImageView *theImageView = [UIImageView new];
[self.view addSubview:theImageView];
theImageView.image = [UIImage imageNamed:#"image1.jpg"];
theImageView.frame = self.view.frame;
theImageView.userInteractionEnabled = YES;
theImageView.alpha = 0.6;
UIImageView *thePanImageView = [UIImageView new];
[self.view addSubview:thePanImageView];
thePanImageView.frame = CGRectMake(0, 0, 100, 100);
thePanImageView.center = CGPointMake(theImageView.frame.size.width/2, theImageView.frame.size.height/2);
thePanImageView.userInteractionEnabled = YES;
thePanImageView.layer.cornerRadius = thePanImageView.frame.size.width/2;
thePanImageView.clipsToBounds = YES;
thePanImageView.layer.borderColor = [UIColor redColor].CGColor;
thePanImageView.layer.borderWidth = 1;
thePanImageView.theWeakObject = theImageView;
{
UIPanGestureRecognizer *thePanGesture = [UIPanGestureRecognizer new];
[thePanGesture addTarget:self action:#selector(handlePanGesture:)];
[thePanImageView addGestureRecognizer:thePanGesture];
UIPinchGestureRecognizer *thePinchGesture = [UIPinchGestureRecognizer new];
[thePinchGesture addTarget:self action:#selector(handlePinchGesture:)];
[thePanImageView addGestureRecognizer:thePinchGesture];
UIImageView *theExtraImageView = [UIImageView new];
[thePanImageView addSubview:theExtraImageView];
theExtraImageView.frame = CGRectMake(-thePanImageView.frame.origin.x, -thePanImageView.frame.origin.y, theImageView.frame.size.width, theImageView.frame.size.height);
theExtraImageView.image = theImageView.image;
}
}
- (void)handlePanGesture:(UIPanGestureRecognizer *)thePanGesture
{
UIImageView *thePanImageView = (id)thePanGesture.view;
UIImageView *theImageView = thePanImageView.theWeakObject;
thePanImageView.center = [thePanGesture locationInView:theImageView];
UIImageView *theExtraImageView = thePanImageView.subviews.firstObject;
theExtraImageView.frame = CGRectMake(-thePanImageView.frame.origin.x, -thePanImageView.frame.origin.y, theExtraImageView.frame.size.width, theExtraImageView.frame.size.height);
}
- (void)handlePinchGesture:(UIPinchGestureRecognizer *)thePinchGesture
{
UIImageView *thePanImageView = (id)thePinchGesture.view;
static CGRect theInitialFrame;
if (thePinchGesture.state == UIGestureRecognizerStateBegan)
{
theInitialFrame = thePanImageView.frame;
}
else
{
CGRect theFrame = theInitialFrame;
theFrame.size.width *= thePinchGesture.scale;
theFrame.size.height *= thePinchGesture.scale;
thePanImageView.frame = theFrame;
}
thePanImageView.layer.cornerRadius = thePanImageView.frame.size.width/2;
UIImageView *theImageView = thePanImageView.theWeakObject;
thePanImageView.center = [thePinchGesture locationInView:theImageView];
UIImageView *theExtraImageView = thePanImageView.subviews.firstObject;
theExtraImageView.frame = CGRectMake(-thePanImageView.frame.origin.x, -thePanImageView.frame.origin.y, theExtraImageView.frame.size.width, theExtraImageView.frame.size.height);
}
FYI:
theWeakObject is just my custom property to NSObject. I used it because I was lazy and didn't want to create globally-visible #property
you should ask for how to output a image use mask! here is the link for the solution:
CGContextClipToMask returning blank image
you need to change:
CGContextClipToRect(context, CGRectMake(0, 0, cropRect.size.width, cropRect.size.height));
to something like:
CGContextClipToMask(context, CGRectMake(0, 0, cropRect.size.width, cropRect.size.height), maskImage);

how to change CropRect when it is loaded?

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;
}

Resize uiscrollview after scaling so as to enable paging

I am having a scrollview containing 2 image views in one page only.I am scaling scrollview so as to cover full screen on tapping.Now i want after scaling, paging should be enabled.But then it skip every alternate image view. Any help would be appreciated.`
kNumViews = 10;
vScrollObjectHeight = 230; // half screen
vScrollObjectWidth = 160; // half screen
{
for (i = 1; i <= kNumViews; i++)
{
NSString *imageName = [NSString stringWithFormat:#"imageV%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[imageView sizeToFit];
imageView.layer.borderColor = [UIColor grayColor].CGColor;
imageView.layer.borderWidth = 1.5f;
CGRect rect = imageView.frame;
rect.size.height = vScrollObjHeight;
rect.size.width = vScrollObjWidth;
imageView.frame = rect;
imageView.tag = i;
imageView.userInteractionEnabled = YES;
[scrollView2 addSubview:imageView];
}
[self layoutScrollViews];
self.swipeRecognizerUp = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(scaleMove:)];
[self.swipeRecognizerUp setDelegate:self];
[scrollView2 addGestureRecognizer:self.swipeRecognizerUp];
self.swipeRecognizerDown = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(scaleDown:)];
[self.swipeRecognizerDown setDelegate:self];
[scrollView2 addGestureRecognizer:self.swipeRecognizerDown];
self.swipeRecognizerDown.enabled = NO;
}
-(void)scaleMove:(id)sender {
[[[(UIPanGestureRecognizer*)sender view] layer] removeAllAnimations];
[self.view bringSubviewToFront:[sender view]];
CGPoint velocity = [sender velocityInView:self.view];
if(abs(velocity.y) - abs(velocity.x) > 500 && velocity.y < 0){
[sender view].transform = CGAffineTransformMakeScale(1.0, 1.0);
//[sender view].transform = CGAffineTransformMakeTranslation(0, 230);
[UIView animateWithDuration:0.5
animations:^(){
CGAffineTransform scale = CGAffineTransformMakeScale(2.0, 2.0);
CGAffineTransform translate = CGAffineTransformMakeTranslation(0,-230);
[sender view].transform = CGAffineTransformConcat(translate, scale);
//NSLog(#"%f %f",[sender view].frame.size.width,[sender view].frame.size.height);
scrollView2.pagingEnabled = YES;
}
completion:nil];
self.swipeRecognizerDown.enabled = YES;
self.swipeRecognizerUp.enabled = NO;
}
}
}
`
found answer of it...I have set the width of scrollview half and then subclasses the scrollview so as to override pointInside and then normally scaled scrollview.found answer of it...I have set the width of scrollview half and then subclasses the scrollview so as to override pointInside and then normally scaled scrollview.

UIScrollView SubViews won't resize

Design:
View Hierarchy is as below:
UIScrollView (for paging multiple image)
UIScrollView (for enabling zoom on the UIImageView)
UIImageView
UIView (origin,size relative to UIImage in UIImageView)
UIView
UIView ...
UIScrollview
UIImageView
UIView
UIView ...
UIScrollView ... and so on
Implementation:
- (id)init
{
self = [super init];
if (self) {
// Custom initialization
self.minimumZoomScale = 1.0f;
self.maximumZoomScale = 4.0f;
self.zoomScale = 1.0f;
self.bouncesZoom = true;
self.bounces = true;
self.tag = 10;
self.alwaysBounceHorizontal = false;
self.autoresizesSubviews = YES;
self.zoomView = [[UIImageView alloc] init];
self.zoomView.userInteractionEnabled = YES;
self.zoomView.autoresizesSubviews = YES;
}
return self;
}
- (void)displayImage:(UIImage *)image
{
// Set the image in the view
[_zoomView setImage:image];
// Set the Frame size to the size of image size
// Ex. Resulting ScrollView frame = {0,0},{1250,1500}
// Ex. Resulting ImageView frame = {0,0}, {1250,1500}
CGRect scrollFrame = self.frame;
scrollFrame.size.width = image.size.width;
scrollFrame.size.height = image.size.height;
self.frame = scrollFrame;
CGRect iViewFrame = _zoomView.frame;
iViewFrame.size.width = image.size.width;
iViewFrame.size.height = image.size.height;
_zoomView.frame = iViewFrame;
// Add subviews before resetting the contentsize
for (customClass* field in list.fields) {
UIView* view = [[UIView alloc] initWithFrame:CGRectMake([field.x floatValue], [field.y floatValue], [field.width floatValue], [field.height floatValue])];
view.backgroundColor = [UIColor redColor];
view.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin |UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[_zoomView addSubview:view];
}
// Set the Frame size to the size of scrollview frame size
// Ex. Resulting ScrollView Frame = {0,0},{320,460}
// Ex. Resulting ImageView Frame = {0,0},{320,460}
CGRect scrollViewFrame = self.frame;
scrollViewFrame.size.width = 320.0f;
scrollViewFrame.size.height = 460.0f;
self.frame = scrollViewFrame;
CGRect imageViewFrame = _zoomView.frame;
imageViewFrame.size.width = self.bounds.size.width;
imageViewFrame.size.height = self.bounds.size.height;
_zoomView.frame = imageViewFrame;
self.contentSize = _zoomView.bounds.size;
[self addSubview:_zoomView];
}
Above is the code that I tried implementing. It adds the UIViews to the UIImageView inside the UIScrollView. But the relative origin of the UIView is not correct (both before and after resizing).
Is there anything I should be doing differently to get the UIViews correctly placed inside the UIImageView?
This was top hit on Google for "scrollview subviews wont resize". For anyone else following that link:
Looking at your source, and comparing to mine, I realised that in Interface Builder, my NIB file had the tickbox for "auto size subviews" unticked. I have a vague memory this might be because in an old version of Xcode (this is an old project that's been maintained for a while) ... UIScrollView defaulted to having it switched off.
So: check that you've got it set correctly in Xcode / Interface Builder, and/or that you're setting it in code.

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