将UIImageView拆分为较小的图像时出现问题

时间:2012-06-12 17:35:11

标签: iphone objective-c ios ipad uiimageview

我正在尝试拍摄UIImageView,调整图像大小并显示它。然后我想将这个图像分解成更小的部分并显示它们。

已调整大小的图像显示正确,但看起来“拆分”图像太大;我认为它们来自原始(略大)的图像。以下屏幕截图显示左侧已调整大小的图像和左侧的一列拆分图像。

enter image description here

调整大小的图像正确显示而较小的图像不会让我感到困惑。任何想法将不胜感激 - 甚至是另类。这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    width = imgOriginal.frame.size.width;
    height = imgOriginal.frame.size.height;
    whratio = width/height; 
    [self getOriginalImageInfo];
    [self resizeImage];
    resizedImage = [self resizeImage];
}

-(void) getOriginalImageInfo {
    lblWidth.text = [NSString stringWithFormat:@"%0.2f", width];
    lblHeight.text = [NSString stringWithFormat:@"%0.2f", height];
    lblWHRatio.text = [NSString stringWithFormat:@"%0.2f", whratio];
}


-(UIImageView*) resizeImage {
    if (whratio >= 0.7 && whratio <=0.89) {
        imgOriginal.frame = CGRectMake(20, 20, 500, 600);
        imgOriginal.autoresizingMask = NO;
        float resizedWHRatio = (imgOriginal.frame.size.width)/(imgOriginal.frame.size.height);
        lblResizedWidth.text = [NSString stringWithFormat:@"%0.2f", imgOriginal.frame.size.width];
        lblResizedHeight.text = [NSString stringWithFormat:@"%0.2f", imgOriginal.frame.size.height];
        lblResizedWHRatio.text = [NSString stringWithFormat:@"%0.2f", resizedWHRatio];
        return imgOriginal;
    }
    return nil;
}

- (IBAction)easyPressed:(id)sender {
    NSLog(@"Easy button pressed");
    CGImageRef original = [resizedImage.image CGImage];
    float pieceWidth = (resizedImage.frame.size.width) / 5;
    float pieceHeight = (resizedImage.frame.size.height) / 6;
    for (int i =0; i<6; i++) {
        CGRect rectangle = CGRectMake(0, 0+((float)i*pieceHeight), pieceWidth, pieceHeight);
        CGImageRef newTile =  CGImageCreateWithImageInRect(original, rectangle);
        UIImageView *puzzlePiece = [[UIImageView alloc] initWithFrame:CGRectMake(611, 20+((float)i*pieceHeight), pieceWidth, pieceHeight)];
        puzzlePiece.tag = i+1;
        puzzlePiece.image = [UIImage imageWithCGImage:newTile];
        puzzlePiece.alpha = 0.5;
        [puzzlePiece.layer setBorderColor: [[UIColor blackColor] CGColor]];
        [puzzlePiece.layer setBorderWidth: 1.0];
        [self.view addSubview:puzzlePiece];
    }
}

1 个答案:

答案 0 :(得分:0)

尝试将相同的缩放系数应用于部分图像,就像使用主图像一样!!!

OR

显示正在显示的UIImageView的屏幕截图并将其拆分。