Is it possible to set the position of an UIImageView's image?

为君一笑 提交于 2019-12-05 11:31:30

问题


I have a UIImageView that displays a bigger image. It appears to be centered, but I would like to move that image inside that UIImageView. I looked at the MoveMe sample from Apple, but I couldn't figure out how they do it. It seems that they don't even have an UIImageView for that. Any ideas?


回答1:


Original Answer has been superseded by CoreAnimation in iOS4.

So as Gold Thumb says: you can do this by accessing the UIView's CALayer. Specifically its contentRect:

From the Apple Docs: The rectangle, in the unit coordinate space, that defines the portion of the layer’s contents that should be used. Animatable.




回答2:


What you need is something like (e.g. showing the 30% by 30% of the top left corner of the original image):

imageView.layer.contentsRect = CGRectMake(0.0, 0.0, 0.3, 0.3);

Description of "contentsRect": The rectangle, in the unit coordinate space, that defines the portion of the layer’s contents that should be used.




回答3:


Do you want to display the image so that it is contained within the UIImageView? In that case just change the contectMode of UIImageView to UIViewContentModeScaleToFill (if aspect ratio is inconsequential) or UIViewContentModeScaleAspectFit (if you want to maintain the aspect ratio)

In IB, this can be done by setting the Mode in Inspector.

In code, it can be done as

yourImageView.contentMode = UIViewContentModeScaleToFill;

In case you want to display the large image as is inside a UIImageView, the best and easiest way to do this would be to have the image view inside a UIScrollView. That ways you will be able to zoom in and out in the image and also move it around.

Hope that helps.




回答4:


It doesn't sound like the MoveMe sample does anything like what you want. The PlacardView in it is the same size as the image used. The only size change done to it is a view transform, which doesn't effect the viewport of the image. As I understand it, you have a large picture, and want to show a small viewport into it. There isn't a simple class method to do this, but there is a function that you can use to get the desired results: CGImageCreateWithImageInRect(CGImageRef, CGRect) will help you out.

Here's a short example using it:

CGImageRef imageRef = CGImageCreateWithImageInRect([largeImage CGImage], cropRect);
[UIImageView setImage:[UIImage imageWithCGImage:imageRef]];
CGImageRelease(imageRef);



回答5:


Thanks a lot. I have found a pretty simple solution that looks like this:

CGRect frameRect = myImage.frame;
CGPoint rectPoint = frameRect.origin;
CGFloat newXPos = rectPoint.x - 0.5f;
myImage.frame = CGRectMake(newXPos, 0.0f, myImage.frame.size.width, myImage.frame.size.height);

I just move the frame around. It happens that portions of that frame go out of the iPhone's view port, but I hope that this won't matter much. There is a mask over it, so it doesn't look weird. The user doesn't totice how it's done.




回答6:


You can accomplish the same by:

UIImageView *imgVw=[[UIImageView alloc]initwithFrame:CGRectMake(x,y,height,width)];     
imgVw.image=[UIImage imageNamed:@""];
[self.view addSubView imgVw];
imgVw.contentMode = UIViewContentModeScaleToFill;


来源:https://stackoverflow.com/questions/710675/is-it-possible-to-set-the-position-of-an-uiimageviews-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!