views:

382

answers:

3

i have picture like this image it will present on Landscape Mode

alt text

so if rotate device to Portrait, how to present image like this

alt text

A: 

You will have to work with view's layer.

You can find the appropriate guidelines at https://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/CoreAnimation_guide/Articles/Layers.html

Hope this helps.

Thanks,
Jim.

Jim
thanks for your information Jim
RAGOpoR
+1  A: 

It looks like what you're trying to do is clip the image on the right, while leaving the top, left, and bottom the same.

Two things to try:

1) Subclass UIView and in the drawRect method, adjust the rect and draw out the clipped image using CGContextDrawImage (context, rect, sourceImage.CGImage).

2) Use CALayers. You can adjust the 'contentsRect' of the imageView's layer:

CALayer* imageLayer = imageView.layer;
imageLayer.masksToBounds = YES;
CGRect rect;
if (inPortrait)
  rect = CGRectMake(0.0, 0.0, 0.5, 1.0); // half size
else 
  rect = CGRectMake(0.0, 0.0, 1.0, 1.0); // full size
imageLayer.contentsRect = rect;

This slices the image in half along width. masksToBounds takes care of clipping sublayers (in case you have them). You can tweak the contentsRect unit rectangle to adjust where you want the cutoff. You may also want to adjust the imageView's own bounds as well to match the size.

This has an added bonus in that adjusting the contentsRect is automatically animated so when you do the rotation the width animates in and out nicely.

Ramin
thanks Ramin ,im trying for your code
RAGOpoR
Ramin did you tell me some tutorial or example code? my code are using UIView and create UIImageView inside, it so complicate to convert my code to your suggestionthanks for all help Ramin
RAGOpoR
Try option #2 above. Plug your UIImageView ivar into the first line of snippet posted above. inPortrait is a BOOL so set it to YES or NO and see if it does what you need it to do. That's probably the easiest way to go.
Ramin
A: 
// Set the layer contents
view.layer.contents = [image CGImage];
// Stretch so that contents overflows the edges and keeps aspect ratio
view.layer.contentsGravity = kCAGravityResizeAspectFill;
// Clip off the overflow
view.layer.masksToBounds = YES;
// Set the frame
CGRect frame;
frame.origin = CGPointZero;
frame.size = view.superview.bounds.size;
view.frame = frame;
// Make sure it resizes when the superview does
view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
rpetrich
thanks rpetrich, could you please tell me more infomation please?i not understand about this variable image and view
RAGOpoR
i have assigen my UIImageView to view and image so it have some error like this "_kCAGravityResizeAspectFill", referenced from:
RAGOpoR
`kCAGravityResizeAspectFill` is in QuartzCore.framework. You will need to reference it. Also, my instructions were for a `UIView` (though I don't see why they couldn't also apply to a `UIImageView`)
rpetrich
Also, `image` is a variable of type `UIImage` that already has the image you want loaded into it. `view` is the `UIView` you want to apply the image to.
rpetrich