Setting pin annotations on an image? - ios

Hi I don't know if this is the right place to ask this question but Im developing an iPhone app and i have an image of a location which I would like for the user to be able to pinch zoom and interact with as if it were a map. I would also want to be able to set pin's to certain location where the user can click the pin and see information about the location.
Pleas If the question lacks information, have the wrong tags or Is not relevant to this forum pleas comment before voting down!!
Thanks"

You have to put your image and your pin in a UIScrollView.
Add main image:
UIImage *image = [UIImage imageNamed:#"image.png"];
imgView.image = image;
CGSize imageSize = image.size;
CGRect rect = CGRectMake(0, 0, imageSize.width, imageSize.height);
imgView.frame = rect;
scrollView.contentSize = CGSizeMake(imgView.frame.size.width, imgView.frame.size.height);
scrollView.maximumZoomScale = 4.0;
scrollView.minimumZoomScale = 1.0;
scrollView.delegate = self;
Then do the same for your pin but change its origin:
pinImageView.frame = CGRectMake(pinX, pinY, pinWidth, pinHeight);
You can find an example here:
UIImageView pinch zooming in UIScrollView

Related

Calculating Visible CGRect to crop a UIImageView [duplicate]

This question already has answers here:
How to crop a UIImageView to a new UIImage in 'aspect fill' mode?
(2 answers)
Closed 4 years ago.
I have a UIView. There is a UIImageView inside that UIView. UIImageView doesn't comply to autolayout constraints. Parent UIView's bounds are clipped.
Now content mode of UIImageView is Aspect Fill, but the size of UIImageView is always calculated and set to size of UIImage that is being rendered. So UIImageView's size will always be equal to size of UIImage.
Now UIImageView can be positioned. It has a pan gesture applied to it. UIImageView is being moved within the parent UIView based on requirement of user.
My task is to crop the UIImage of UIImageView to the visible viewport of parent UIVIew only. Core Cropping the image is not issue but finding the visible rect is.
My solution which is not working was to convert frame of parent UIView and child UIImageView to window screen. Then find the intersection of new converted frames. But this is not working. There should be some simple mathematics behind this which I am not able to come up successfully.
This is my code so far.
CGRect imgFrame = [_pageImageView convertRect:_pageImageView.frame toView:nil];
CGRect viewportFrame = [_viewportView convertRect: _viewportView.frame toView:nil];
CGRect visibleRect = CGRectIntersection(imgFrame, viewportFrame);
CGImageRef imageRef = CGImageCreateWithImageInRect([_pageImageView.image CGImage], visibleRect);
UIImage *cropped = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
My objective is to find the visible rect that is visible through clipped parent UIVIew, then crop the UIImageView based on that visible rect. Important note: UIImageView can be positioned and moved within parent UIView.
EDIT: I can't use UIScrollView for cropping as used in many other discussions for my internal reasons.
You can place the image view inside the UIScrollView instead of UIView which make the pan gesture work easier.
I have the scrollview have the UIImageView
- (CGRect)imageCropRect
{
CGSize imageSize = self.image.size;
CGSize contentSize = self.contentSize;
CGRect cropBoxFrame = self.frame;
CGPoint contentOffset = self.contentOffset;
UIEdgeInsets edgeInsets = self.contentInset;
CGRect frame = CGRectZero;
frame.origin.x = floor((contentOffset.x + edgeInsets.left) * (imageSize.width / contentSize.width));
frame.origin.x = MAX(0, frame.origin.x);
frame.origin.y = floor((contentOffset.y + edgeInsets.top) * (imageSize.height / contentSize.height));
frame.origin.y = MAX(0, frame.origin.y);
frame.size.width = ceil(cropBoxFrame.size.width * (imageSize.width / contentSize.width));
frame.size.width = MIN(imageSize.width, frame.size.width);
frame.size.height = ceil(cropBoxFrame.size.height * (imageSize.height / contentSize.height));
frame.size.height = MIN(imageSize.height, frame.size.height);
return frame;
}

Issue with subviews overlapping

I'm trying to create a custom view which containes several images. I do that by adding them programmatically. The problem is that those subviews overlap each other and I can't find the way to change that. The only solution I can see is doing something like setting frames for each new image programmatically. I would be grateful if someone could tell me what is the best way to solve this issue.
for (id image in self.images) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.imageViews addObject:imageView];
[self addSubview:imageView];
}
If you wanna make your customView like UICollectionView you need a UIScrollView and add your subviews in it. Everytime when you add a subview change frame location so it could be something like this:
int xPosition = 0;
int yPosition =0;
for (id image in self.images) {
if (xPosition>self.view.frame.size.width) {
//give size to every imageView and every time in loop change the location
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xPosition, yPosition, 50, 50)];
imageView.image = image;
yPosition = yPosition + 50;
[self.view addSubView:imageView];
}
else {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xPosition, yPosition, 50, 50)];
imageView.image = image;
xPosition = xPosition + 50;
[self.view addSubView:imageView];
}
}
Without using Interface Builder your only real options are to change the frame or the center.
imageView.frame = CGRectMake(x coord, y coord, width, height);
This method lets you resize and move whereas changing the center lets you do just that, move the center of the view.
or
imageView.center = CGPointMake(x coord, y coord);
Or as recommended add constraints.

Camera Overlay not in the center

I am trying to add an overlay to this UIImagePickerController instance but the result is the overlay off center.
I have created a simple red square (300x300 pt) to exemplify the problem and serve as an overlay and added that to the camera overlay, using this:
CGRect bounds = [[UIScreen mainScreen] bounds];
UIView *containerOverlay = [[UIView alloc] initWithFrame:bounds];
UIImage *overlay = [UIImage imageNamed:#"red"];
UIImageView *overlayView = [[UIImageView alloc] initWithImage:overlay];
CGFloat midX = CGRectGetMidX(bounds);
CGFloat midY = CGRectGetMidY(bounds);
CGPoint center = CGPointMake(midX, midY);
overlayView.center = center;
[containerOverlay addSubview:overlayView];
[_camera setCameraOverlayView:containerOverlay];
this is what is shown on the live preview... completely off the vertical center.
amazingly, when I take the picture and iOS shows me the picture already taken, the overlay is magically centered...
How do I center the overlay on the live camera?
Well the overlay is y-centered to the screen bounds right?
Which is what you are doing here:
CGFloat midX = CGRectGetMidX(bounds);
CGFloat midY = CGRectGetMidY(bounds);
If you want the overlay aligned to the actual image rect you need to offset the bottom black bar (it's not the same height as the top black bar).

capture screen shot of full content of web view

I am new in iOS. I want to capture screen shot of full content of web view, as webview may have webpage longer that could be scrollable and so not showing the full content in length more than screen size. I am using below code like:-
UIImage *screenImage=[[UIImage alloc] init];
UIScrollView *browserScrollableView=[[UIScrollView alloc] init];
browserScrollableView=browserView.scrollView;
UIGraphicsBeginImageContext(browserScrollableView.contentSize);
CGPoint savedContentOffset = browserScrollableView.contentOffset;
CGRect savedFrame = browserScrollableView.frame;
browserScrollableView.contentOffset = CGPointZero;
browserScrollableView.frame = CGRectMake(0, 0, browserScrollableView.contentSize.width, browserScrollableView.contentSize.height);
[browserScrollableView.layer renderInContext: UIGraphicsGetCurrentContext()];
screenImage = UIGraphicsGetImageFromCurrentImageContext();
browserScrollableView.contentOffset = savedContentOffset;
browserScrollableView.frame = savedFrame;
UIGraphicsEndImageContext();
NSLog(#"Captured image size is %f X %f",screenImage.size.width,screenImage.size.height);
UIImageWriteToSavedPhotosAlbum(screenImage, nil, nil, nil);
browserview is the webview. It's working on simulator why is it not working on device?

iOS: ImageView filled with a picture

I have a table that contains the path to the picture, saved on user's library. Then, I pass this path to a View that contains just a imageView. I want to fills this ImageView with the picture. Like WhatsApp (when you click on a profile's picture). But, my picture it's always cropped or distorted. I tryed different ways to do this, but I didn't find the best way:
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.translucent = NO;
UIImage *picture = [UIImage imageWithContentsOfFile: self.picturePath]; //I passed this path from the previous View
UIImageView* imageView = [[UIImageView alloc] initWithImage:picture];
CGRect screenRect = [[UIScreen mainScreen] bounds];
imageView.frame = CGRectMake(0, 0, screenRect.size.width, screenRect.size.height);
imageView.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:imageView];
}
Original (I want something like it)
My app
If your image is cut , the reason is you are using a higher resolution image in a smaller container and instructed your imageview to fill , using this line imageView.contentMode = UIViewContentModeScaleAspectFill;
Change it to imageView.contentMode = UIViewContentModeScaleAspectFit;
I think #whitewolf09 is right and his method will solve your problem. But i suggest you to look into MGImageUtilities that will be very useful for different cases where you can crop images maintaining aspect ratio to fit inside your image view's frame.
Just #import "UIImage+ProportionalFill.h"
UIImage *image = [[UIImage imageWithContentsOfFile:filePath] imageScaledToFitSize:imageView.frame.size];
That's the very useful category for image resizing and may help you in future.
I think the reason it is being clipped is that you are creating the image view with a picture THEN setting the frame and probably the picture is bigger than the frame so it is clipping it. Also try changing the aspect to: 'UIViewContentModeScaleAspectFit' Try this:
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.translucent = NO;
UIImage *picture = [UIImage imageWithContentsOfFile:self.picturePath]; //I passed this path from the previous View
UIImageView* imageView = [[UIImageView alloc] initWithRect:CGRectMake(0, 0, screenRect.size.width, screenRect.size.height);];
CGRect screenRect = [[UIScreen mainScreen] bounds];
imageView.image = picture;
imageView.contentMode = UIViewContentModeCenter;
[self.view addSubview:imageView];
}

Resources