开发者

Show middle of Image on iPhone - Cocoa

I am getting into a dilemma. I am trying to show the middle of an image. I use the the mageWithImage:CovertToSize function to scale the image. The problem is that the image is being stretched height wise. So the question is, what is the correct method to scale an image to 320 pixels wide and show the middle of the photo or close to it?

    UIImage *image = [[UIImage alloc] initWithData:imageData];
    Scale *scale = [[Scale alloc] init];
    float newHeight = (image.size.height * 2) / image.size.width * 320.0f;
    image = [scale2 imageWithImage:image CovertToSize:CGRectMake(0, -(newHeight / 2), 320.0f, newHeight)];
    CGImageRef imageRef;
    if (newHeight > 125) {
        imageRef = CGImageCreateWithImageInRect([image CGImage], CGR开发者_StackOverflowectMake(0, 0, 320, 125));
    }else{
        imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectMake(0, 0, 320, newHeight));
    }
    NSData *imgData = UIImageJPEGRepresentation([UIImage imageWithCGImage:imageRef], 1.0);
    UIImage *cimg = [UIImage imageWithData:imgData];


If your not aware of how layers work, http://www.ruttencutter.com/?p=90 If you are, I suggest you have a frame of an image as a layer with the selected area in the middle cut out so that, under that layer, you can put the image there so that you'll only see that selected area.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜