select part of image (clipping)

2019-03-30 00:14发布

问题:

This is probably not as hard as I think it is, but how can I select part of an image? Consider the following example:

alt text http://img683.imageshack.us/img683/2680/imagepart.jpg

The grey area is an image in the PNG or JPG Format, now I want to select the red 80x80 px area from it. The red area should be displayed, the rest not. I have tried numerous approaches:

  • Make clipsToBounds of UIImageView to YES and the try to offset the image (doesn't work)
  • Clip the upper view (doesn't work)

I also had a look at the drawing functions, yet I have not worked with these yet and it seemed a little bit far fetched to me. Is there any obvious way for doing what I want to do that I am missing out on? I do not want the image to be resized, just clipped.

Thanks for your time!

回答1:

If you only want to show the center of the image an a UIImageView set the content mode to "Aspect Fill" via IB. That will center it and crop off anything extra.

Or in code

myImageView.contentMode = UIViewContentModeScaleAspectFill;


回答2:

Stole the code below from this question and look for HitScan's answer.

CGImageRef imageRef = CGImageCreateWithImageInRect([largeImage CGImage], cropRect);
// or use the UIImage wherever you like
[UIImageView setImage:[UIImage imageWithCGImage:imageRef]]; 
CGImageRelease(imageRef);


回答3:

myImageView.contentMode = UIViewContentModeCenter;
myImageView.clipsToBounds = YES;


回答4:

Refer to the code below. Here i am providing an imageview and splitting the same in two equal imageviews.

- (void)splitImageView:(UIImageView*)imgView{

UIImage *image = imgView.image;
CGRect rectForDrawing = CGRectMake(0, 0, imgView.frame.size.width/2, imgView.frame.size.height);

CGRect leftRect = CGRectMake(0, 0, imgView.frame.size.width/2, imgView.frame.size.height);
CGRect rightRect = CGRectMake(imgView.frame.size.width/2, 0, imgView.frame.size.width/2, imgView.frame.size.height);


CGImageRef leftReference =  CGImageCreateWithImageInRect([image CGImage], leftRect);
CGImageRef rightReference = CGImageCreateWithImageInRect([image CGImage], rightRect);

UIGraphicsBeginImageContextWithOptions(rectForDrawing.size, NO, 0);
    CGContextRef con = UIGraphicsGetCurrentContext();
    CGContextDrawImage(con, rectForDrawing,flip(leftReference));
    imgViewLeft = [[UIImageView alloc] initWithImage:UIGraphicsGetImageFromCurrentImageContext()];
    imgViewLeft.frame = CGRectMake(imgView.frame.origin.x, imgView.frame.origin.y,
                               imgView.frame.size.width/2, imgView.frame.size.height);

    CGContextDrawImage(con, rectForDrawing,flip(rightReference));
    imgViewRight = [[UIImageView alloc] initWithImage:UIGraphicsGetImageFromCurrentImageContext()];
    imgViewRight.frame = CGRectMake(imgView.frame.origin.x+imgView.frame.size.width/2, imgView.frame.origin.y,
                                imgView.frame.size.width/2, imgView.frame.size.height);


UIGraphicsEndImageContext();


[self.view addSubview:imgViewLeft];
[self.view addSubview:imgViewRight]; 
}