簡體   English   中英

通過使用UIGraphicsBeginImageContext和drawRect計算圖像的大小和位置來在iOS中拼貼imageView

[英]calculatiing Image size & position by using UIGraphicsBeginImageContext & drawRect to collage imageView in ios

我正在嘗試使用UIGraphicsBeginImageContext創建拼貼圖像。 但是裁剪圖像的特定位置並將imageView放置在視圖中的適當位置會讓我發瘋。 到目前為止,我的嘗試:

-(void) viewWillAppear:(BOOL)animated
{
    [self noOfImages:2]; // no of images method used for how many image you want to show in the view. Currently I needed a collage imageView upto 3 images
}

-(void)noOfImages:(int)num
{
    if (num==2)
    {
        UIImageView *One=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, containerView.frame.size.width, containerView.frame.size.height)];
        [One setBackgroundColor:[UIColor grayColor]];
        [One setImage:[UIImage imageNamed:@"1.jpg"]];

        UIImageView *Two=[[UIImageView alloc] initWithFrame:CGRectMake(containerView.frame.size.width/2, 0, containerView.frame.size.width/2, containerView.frame.size.height)];
        [Two setBackgroundColor:[UIColor grayColor]];

        NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"jpg"];
        UIImage *existingImage = [[UIImage alloc] initWithContentsOfFile:imagePath];


        // Create new image context (retina safe)
         UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 200), NO, [UIScreen mainScreen].scale);

         // Create rect for image
         CGRect rect = CGRectMake(1, 0, containerView.frame.size.width, containerView.frame.size.height);

         // Draw the image into the rect
         [existingImage drawInRect:rect];

         // Saving the image, ending image context
         UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
         UIGraphicsEndImageContext();

        [Two setImage:newImage];


        [containerView addSubview:One];
        [containerView addSubview:Two];
    }

    else if (num==3)
    {
        UIImageView *One=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, containerView.frame.size.width, containerView.frame.size.height)];
        [One setBackgroundColor:[UIColor grayColor]];
        [One setImage:[UIImage imageNamed:@"1.jpg"]];

        UIImageView *Two=[[UIImageView alloc] initWithFrame:CGRectMake(containerView.frame.size.width/2, 0, containerView.frame.size.width/2, containerView.frame.size.height/2)];
        [Two setBackgroundColor:[UIColor grayColor]];

        NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"jpg"];
        UIImage *existingImage = [[UIImage alloc] initWithContentsOfFile:imagePath];


        // Create new image context (retina safe)
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 200), NO, [UIScreen mainScreen].scale);

        // Create rect for image
        CGRect rect = CGRectMake(1, 0, containerView.frame.size.width, 200);

        // Draw the image into the rect
        [existingImage drawInRect:rect];

        // Saving the image, ending image context
        UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        [Two setImage:newImage];

        UIImageView *Three=[[UIImageView alloc] initWithFrame:CGRectMake(containerView.frame.size.width/2, containerView.frame.size.height/2, containerView.frame.size.width/2, containerView.frame.size.height/2)];
        [Three setBackgroundColor:[UIColor grayColor]];

        NSString *imagePath3 = [[NSBundle mainBundle] pathForResource:@"3" ofType:@"jpg"];
        UIImage *existingImage3 = [[UIImage alloc] initWithContentsOfFile:imagePath3];


        // Create new image context (retina safe)
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 200), NO, [UIScreen mainScreen].scale);

        // Create rect for image
        CGRect rect3 = CGRectMake(1, 0, containerView.frame.size.width, 200);

        // Draw the image into the rect
        [existingImage3 drawInRect:rect3];

        // Saving the image, ending image context
        UIImage * newImage3 = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        [Three setImage:newImage3];

        [containerView addSubview:One];
        [containerView addSubview:Two];
        [containerView addSubview:Three];
    }
}

兩個圖像的輸出應為:

在此處輸入圖片說明

三個圖像的輸出應為:

在此處輸入圖片說明

實際上,當我使用200x200的圖像時,輸出是正確的,但是此代碼不適用於高度和寬度未知的動態圖像。 此外,右側圖像稍微拉伸了一點,並且不位於拖曳圖像​​的正確位置。右側圖像的x位置應從中間點開始到結束,而不是從起點到中間。 我知道我的計算是錯誤的。 但是不能使用UIGraphicsBeginImageContextWithOptions或作為子視圖添加的imageview來設置適當的高度,寬度,位置或裁剪。

我需要像這樣的輸出:對於兩個圖像-第一個圖像的一半在左邊,另一個圖像的一半在右邊。 對於三張圖片,第一張圖片的一半在左側,第二張圖片的一半在右上方,第三張圖片的右下一半。

按比例放大第二個圖像,然后將其放置在應有的位置。 您可以更輕松地直接在項目中對其進行編碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM