Reputation:
I have created a UIImageview programmatically, and I am showing an image from a string which contains url of a picture
But I wish to show a part(a rectangle at center) of the original image. How do I achieve this?
NSString * urlString = @"http://4.bp.blogspot.com/- rEqVgkdnuxE/TWd6Fm6EWtI/AAAAAAAABSc/cWCehI51v_Y/s1600/red_rose_flower3.jpg"
NSURL *url = [NSURL URLWithString:urlstring];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
Upvotes: 1
Views: 153
Reputation: 10096
The very simple way to move big image inside UIImageView as follows.
Let we have the image of size (imgX, imgY) in pixels and we want to show its center in the frame of size (sizeX, sizeY) in points. The solution is:
UIImageView *iView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, sizeX, sizeY)];
iView.image = [UIImage imageNamed:@"NAME"];
CGRect contentFrame = CGRectMake(1/2-sizeX/imgX, 1/2-sizeY/imgY, 2*sizeX/imgX, 2*sizeY/imgY);
iView.layer.contentsRect = contentFrame;
Here contentFrame is normalized frame relative to real UIImage size. It will be the original view of image without magnifications.
Upvotes: 0
Reputation: 18551
Try this on for size :)
UIImageView *imageView = [UIImageView alloc] initWithImage:image];
[imageView setFrame:SIZEOFRECTANGLE];
[imageView setContentModeCenter];
Upvotes: 1