本文介紹了如何從 UIView/UIScrollView 創(chuàng)建圖像的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我在 UIScrollView 中有一張圖片,可以滾動和縮放.
I have an image in an UIScrollView, that can be scrolled and zoomed.
當用戶按下按鈕時,我希望代碼從我用 CGRect 指定的區(qū)域內的 UIScrollView 的任何部分創(chuàng)建圖像.
When the user presses a button, I want the code to create an image from whatever part of the UIScrollView is inside an area I specify with a CGRect.
我見過裁剪 UIImages 的代碼,但我無法調整它來為視圖做同樣的事情,因為它使用 CGContextDrawImage.
I've seen code to crop UIImages, but I can't adapt it to do the same for a view, because it uses CGContextDrawImage.
有什么想法嗎?
干杯,安德烈
推薦答案
我已經(jīng)搞定了.
這是我的解決方案,基于網(wǎng)絡上的幾個不同的解決方案:
Here's my solution, based on a few different ones from the web:
- (UIImage *)imageByCropping:(UIScrollView *)imageToCrop toRect:(CGRect)rect
{
CGSize pageSize = rect.size;
UIGraphicsBeginImageContext(pageSize);
CGContextRef resizedContext = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(resizedContext, -imageToCrop.contentOffset.x, -imageToCrop.contentOffset.y);
[imageToCrop.layer renderInContext:resizedContext];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
您使用以下方式調用:
CGRect clippedRect = CGRectMake(0, 0, 320, 300);
picture.image = [self imageByCropping:myScrollView toRect:clippedRect];
這篇關于如何從 UIView/UIScrollView 創(chuàng)建圖像的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!