UIImageView with another UIImage of frame - objective-c

i m developing an photo based app
i have two imageview one imageview contains the original image
and other imageview contains the frame
Now the problem is i want to manage the frame Imageview according to the main image
i.e. if my main image view is of size 320x480 then border applies on that size
and if my image view is of size 200x150 then border have to applies on that size
How to do that

UIImage.size can give you dimensions of image you are using. you can resize your UIImageView.frame using this size. this will fit UIImage in UIImageView without any extra space so borders will appear perfect.
second option could be to initialize an image view once again using following method.
- (id)initWithImage:(UIImage *)image
this will make a perfect sized imageview for your image.
Let me know if you are not looking for something like this....

Related

NSImageView resized with image size

I'm new to Obj-C/Cocoa programming and facing to a problem about NSImageView.
My goal is to create an app which allow the user to put an image on it with drag&drop.
The problem is the following : when we're dropping an image larger than the window, it resizes the window to the image size, I don't want that !
I put my NSImageView into a NSScrollView so only a certain part of the image MUST be showed and the user will move sliders to view the entire one (or simply resize the window), that's what I want..
Here is my XIB hierarchy (concerned part) :
View
NSScrollView
View
NSImageView
In the code of my NSImageView, I have (about that problem) :
[self setImageScaling:NSScaleProportionally];
[self setImage:newImage];
NOTE: The sliders seem to move the view and not the image
NOTE2: I can't resize the window to a lower size than the image
I have a horrible aversion to nib files and prefer to do things programmatically whenever possible so I'm not much help with that... but here's a little listing from a little NSScrollview test app I recently coded up that does something similar. The only non-self-contained thing mentioned in this code chunk is the window name, which refers to an NSWindow * for a window that was already created. And self here just refers to a controller object.
NSImage * myImage = [NSImage imageNamed:#"myfilename.png"];
NSRect imageRect = NSMakeRect(0.0,0.0,myImage.size.width,myImage.size.height);
self.myImageView = [[NSImageView alloc] initWithFrame:imageRect];
self.myImageView.bounds = imageRect;
self.myImageView.image = myImage;
self.myScrollView = [[NSScrollView alloc] initWithFrame:[window.contentView frame]];
self.myScrollView.hasVerticalScroller = YES;
self.myScrollView.hasHorizontalScroller = YES;
self.myScrollView.documentView = self.myImageView;
self.myScrollView.borderType = NSNoBorder;
self.myScrollView.scrollerStyle = NSScrollerStyleOverlay;
window.contentView = self.myScrollView;
I know that's perhaps not the solution you were looking for but maybe it helps you debug? In particular, I didn't do anything funny with my NSImageView besides give it a frame, a bounds rectangle, and an NSImage *, so I don't think you need to worry about the scaling parameter per se -- in fact, if you want the image to be full-sized and to scroll around it, you want your NSImageView's frame to be the same size as the image so that it doesn't HAVE to scale at all. Maybe that's your problem -- the frame is too small? It is OK for the NSImageView frame to be bigger than the window -- that's what lets you scroll around it.
To me it sounds like your views don't have the proper sizes. Because of bouncing and because your views don't clip their subviews, you can still scroll a bit, but it returns to its original position.
Check if the NSImageView is big enough to display your full image. (Check frame or bounds for width and height)
If it is not big enough do this (inside the method, where you set the new image):
self.myImageView.frame = NSMakeRect(0.0,0.0, self.myImageView.image.size.width, self.myImageView.image.size.height);
Adjust the size of your scroll views view accordingly.
self.myScrollView.view.frame = self.myImageView.frame;
Now your scrollable area should be big enough to cover the full image.
If this works for you, give Matt some credit as well, by up-voting his answer, since his answer does essentially the same thing - only code based, instead of initialising everything using a nib file - and the code I used is very similar to what he had already posted.
I suggest to put the "image view" inside of the custom view (edges to its superview).
The image view's constraints to the equal edges, except the bottom constraint, which to the "Less Than or Equal" relation to its superview.

How can I trim a UIImageView to fit an aspect ratio image

I am using a crop tool in my app and I need to modify a UIImageView so that it fits an image exactly after inserting the image in aspect fit mode.
So an image is selected and added to the UIImageView in aspect fit mode. The problem is that this then leaves "blank space" around the image inside the UIImageView that needs trimming. I was wondering how I could then go and resize the holding UIImageView based upon the image inside.
Is this possible?
The Easy way is simply using the following code on your "imageView"
imageView.contentMode = UIViewContentModeScaleAspectFit;
Assuming you want to cut a "zoomed" section of your image to fit fully into your imageView
check your original image width and height
Assuming width is bigger in size then height , scale the image width to the holder width
center the image on your holder , the width will fit perfectly (section2) and the height will simply be cropped follow above and below the holder.
It turns out that a better approach is to use the following idea.
How to get the size of a scaled UIImage in UIImageView?
Instead of trimming the UIImageView, insert the image and then get the dimensions of the image inside the UIImageView, from there you can then resize the UIImageView to match the dimensions of the image inside.

Can a UIViewImage draw its image outside its frame?

When making a photo viewer app found that our UIImageView controller is drawing its image outside its frame when the content mode is different neither ScaleToFill nor Aspect Fit.
Trying to understand why; I isolated the problem making a new project which only has a UIImageView with the following frame (50,50,100,100). The image size contained in it is (4592,3056).
After running the app, with the content mode set to ScaleToFill and AspectFit it all worked as expected:
But after setting the contentMode of the UIImageView to TopLeft, the image is drawn outside its frame, the odd thing is that the log from the frame after all has been drawn is still the original {50,50,100,100}.
I've try to understand the issue by moving the Autoresize, the clips and the content mode of the UIViewController but the result is the same.
set clipToBounds = YES on the view.
its NO by default because that makes drawing way cheaper

Having a subview that shows a portion of an image contained in its superview

I'm trying to create a 'monocle' view of sorts that you can move around and scale that will show a section of a larger image (section size is defined by the monocle's bounds). The larger image should not show beyond the bounds the monocle.
Right now I have a UIImageView (called lensImage) which is a subview of a UIView named lens.
I was attempting to use a 2D CGAffineTransform to show the relevant section of an image based off of the monocle's position, but that would snap the image to odd positions.
Any tips on what I need to work with?
Here is a diagram explaining the desired setup:

Make NSView resizable WITHOUT resizing images contained within

I have an NSView that resizes automatically based on the size of the window. I load images and display them in the NSView, but I do not want the NSView to try and "squeeze" them to fit the NSView's size. What I would like is to have the images load into the NSView at 100%, then resize the window to reveal more of the image rather than scale it to fit. I may want to modify this later to allow zooming and panning as well.
Here's what I did:
-(void)drawRect:(NSRect)rect
{
NSRect theRect = NSMakeRect((rect.size.width/2)-(([theImage pixelsWide]/2)/2),
(rect.size.height/2)-(([theImage pixelsHigh]/2)/2),
[theImage pixelsWide]/2,
[theImage pixelsHigh]/2);
[theImage drawInRect:theRect];
}
How are you displaying the images in the first place? Either you're using an NSImageView or you're drawing the images into your custom view yourself in -drawRect:.
If you're using an image view, take a look at its autosizing settings (its geometry as its parent view's geometry changes) and its image scaling properties (the image size and ratio as the image view's geometry changes.
If a custom view then draw the image into whatever rectangle you please rather than the view's entire bounds.