0

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

1 に答える 1

0

メイン画像と同じ倍率を部分画像に適用してみてください!!!

また

表示されている UIImageView のスクリーンショットを撮り、代わりに分割します。

于 2012-06-12T17:40:41.783 に答える