[英]Convert CGPoint from CGRect to other CGRect
我在視圖上放置了一個標簽,視圖的框架是CGRectMake(0,0,270,203) 。 現在我必須使用CGRectMake(0,0,800,600)拍攝視圖的屏幕截圖。 所以我必須將標簽從舊的rect轉換為新的rect 。
這是我以前拍攝屏幕截圖的代碼:
CGRect rect = [view bounds];
UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
這是我用來轉換點的代碼:
CGRect f = [view convertRect:lblMessage.frame fromView:viewMessage];
但我無法從新圖像中獲得標簽的實際位置。 任何人都可以幫助我錯了。
在這里,我附上圖片以獲得更多說明。 在小視圖中,我添加了一個標簽,我必須根據大圖像視圖轉換標簽的框架。
謝謝,
+(UIImage*)screenShotOf:(UIView*)view atScale:(CGFloat)scale
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, scale);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
在這里您需要根據需要調整scale屬性。
你可以這樣做:
-(CGPoint) convertPoint: (CGPoint) point fromRect: (CGRect) fromRect toRect: (CGRect) toRect {
return (CGPoint){
(toRect.size.width/fromRect.size.width) * point.x,
(toRect.size.height/fromRect.size.height) * point.y
};
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.