【问题标题】:Having problems splitting up UIImageView into smaller images将 UIImageView 拆分为较小的图像时遇到问题
【发布时间】:2012-06-12 17:35:11
【问题描述】:

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

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

调整大小的图像正确显示而较小的图像没有让我感到困惑。任何想法都会受到赞赏 - 甚至是替代方案。这是我的代码:

- (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];
    }
}

【问题讨论】:

    标签: iphone objective-c ios ipad uiimageview


    【解决方案1】:

    尝试对部分图像应用与主图像相同的缩放因子!!!

    或

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

    【讨论】:

    • 我很确定我正在使用resizedImage 作为分割的基本图像来做你的第二个选择。
    • UIImageView 不会调整基本图像的大小,它会缩放/调整副本以进行显示,但会保留原始图像的缓冲。所以当你去获取 UIImageView.image 时,它​​会返回给你原来的。因此,您正在拆分图像的更大(非显示)版本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-26
    • 1970-01-01
    • 2014-07-17
    相关资源
    最近更新 更多