您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 程式設計技術

iOS 圖片截取與拼接:四個實用方法整理

編輯:iOS 程式設計技術

截圖與拼圖是很常見的需求——分享長圖、生成訂單海報、把頭圖正文尾部合成一張。核心都離不開 UIGraphicsBeginImageContext 這套離屏繪圖 API。

一、截取整個 View

思路是遍歷 view 的 layer 並渲染到當前上下文。如果傳入的是 UIScrollView,還要特別把尺寸換成 contentSize,這樣才能截到滾動區域之外的部分。

-(UIImage*)captureView:(UIView *)theView{
    CGRect rect = theView.frame;
    if ([theView isKindOfClass:[UIScrollView class]]) {
        rect.size = ((UIScrollView *)theView).contentSize;
    }
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [theView.layer renderInContext:context];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

二、截取 View 上的某一塊區域

先截全屏,再按指定 rect 裁剪。這裡用的是 CGImageCreateWithImageInRect,用完記得 CGImageRelease 釋放,否則會內存洩漏。

-(UIImage *)cropImage:(UIImage *)image rect:(CGRect)cropRect {
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);
    UIImage *img = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    return img;
}

三、截取 ScrollView 的完整內容

這個方法比第一個更完整:它會先備份當前的 contentOffset 和 frame,把 scrollView 臨時擺正到原點並放大到 contentSize,渲染完再恢復原狀,用戶完全感知不到。

- (UIImage *)captureScrollView:(UIScrollView *)scrollView{
    UIImage* image = nil;
    UIGraphicsBeginImageContext(scrollView.contentSize);
    {
        CGPoint savedContentOffset = scrollView.contentOffset;
        CGRect savedFrame = scrollView.frame;

        scrollView.contentOffset = CGPointZero;
        scrollView.frame = CGRectMake(0, 0,
                                      scrollView.contentSize.width,
                                      scrollView.contentSize.height);

        [scrollView.layer renderInContext:UIGraphicsGetCurrentContext()];
        image = UIGraphicsGetImageFromCurrentImageContext();

        scrollView.contentOffset = savedContentOffset;
        scrollView.frame = savedFrame;
    }
    UIGraphicsEndImageContext();

    if (image != nil) {
        return image;
    }
    return nil;
}

四、多張圖片拼接

拼接的關鍵是先把總尺寸算對:寬度取內容圖寬度,高度是三段高度的累加。然後依次用 drawInRect: 把每張圖畫到正確的 y 偏移上。

- (UIImage *)composeWithHeader:(UIImage *)header
                       content:(UIImage *)content
                        footer:(UIImage *)footer{
    CGSize size = CGSizeMake(content.size.width,
                             header.size.height + content.size.height + footer.size.height);
    UIGraphicsBeginImageContext(size);

    [header drawInRect:CGRectMake(0, 0, header.size.width, header.size.height)];
    [content drawInRect:CGRectMake(0, header.size.height,
                                   content.size.width, content.size.height)];
    [footer drawInRect:CGRectMake(0, header.size.height + content.size.height,
                                  footer.size.width, footer.size.height)];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

五、幾點實踐建議

1. 高解析度截圖

上面的方法在 Retina 屏上輸出的實際像素是 size 的 2 倍或 3 倍,這是系統 scale 帶來的,通常正是我們想要的。如果希望明確控制,可以改用 UIGraphicsBeginImageContextWithOptions(size, NO, 指定scale)。

2. 拼接更通用的寫法

三段式拼接寫死太死板。實際項目中經常是任意張數,可以改成傳入圖片數組,遍歷時累加 y 偏移:

- (UIImage *)composeImages:(NSArray<UIImage *> *)images {
    CGFloat totalHeight = 0;
    CGFloat maxWidth = 0;
    for (UIImage *img in images) {
        totalHeight += img.size.height;
        maxWidth = MAX(maxWidth, img.size.width);
    }
    UIGraphicsBeginImageContext(CGSizeMake(maxWidth, totalHeight));
    CGFloat y = 0;
    for (UIImage *img in images) {
        [img drawInRect:CGRectMake(0, y, img.size.width, img.size.height)];
        y += img.size.height;
    }
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return result;
}

3. 記得配對 End

每一個 UIGraphicsBeginImageContext 都必須有對應的 UIGraphicsEndImageContext。漏寫會導致上下文堆棧失衡,後續繪圖全部出錯,這類 bug 排查起來非常痛苦。

4. 注意主線程壓力

截長圖、拼大圖都是重活。圖片特別多或特別長時,建議放到子線程處理,完成後回主線程更新 UI。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved