获取 UIScrollView 的可见内容时额外的白色 space
Extra white space when get visible content of UIScrollView
UIScrollView
里面有一个UIImageView
,用户可以缩小和放大然后保存:
导航栏下方和标签栏顶部的区域是UIScrollView
框架。
当用户点击 完成 时,图像将保存在相机胶卷中。这是保存在那里的内容(照片应用程序):
我不知道保存的图像中这个空白 space 是什么。
这是我保存图片的代码:
UIGraphicsBeginImageContextWithOptions(scrollView.frame.size, false, 0.0)
let rect = CGRectMake(0, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height)
self.view.drawViewHierarchyInRect(rect, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
let imageData = UIImageJPEGRepresentation(image, 1)
let compressedJPGImage = UIImage(data: imageData!)
UIImageWriteToSavedPhotosAlbum(compressedJPGImage!, nil, nil, nil)
我要保存的正是UIScrollView
的可见区域。
我不得不使用 CGContextTranslateCTM()
来平移矩形:
let screenRect: CGRect = scrollView.bounds
UIGraphicsBeginImageContext(screenRect.size)
let ctx: CGContextRef = UIGraphicsGetCurrentContext()!
CGContextTranslateCTM(ctx,0,-scrollView.frame.origin.y)
UIColor.blackColor().set()
CGContextFillRect(ctx, screenRect)
view.layer.renderInContext(ctx)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
UIScrollView
里面有一个UIImageView
,用户可以缩小和放大然后保存:
导航栏下方和标签栏顶部的区域是UIScrollView
框架。
当用户点击 完成 时,图像将保存在相机胶卷中。这是保存在那里的内容(照片应用程序):
我不知道保存的图像中这个空白 space 是什么。
这是我保存图片的代码:
UIGraphicsBeginImageContextWithOptions(scrollView.frame.size, false, 0.0)
let rect = CGRectMake(0, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height)
self.view.drawViewHierarchyInRect(rect, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
let imageData = UIImageJPEGRepresentation(image, 1)
let compressedJPGImage = UIImage(data: imageData!)
UIImageWriteToSavedPhotosAlbum(compressedJPGImage!, nil, nil, nil)
我要保存的正是UIScrollView
的可见区域。
我不得不使用 CGContextTranslateCTM()
来平移矩形:
let screenRect: CGRect = scrollView.bounds
UIGraphicsBeginImageContext(screenRect.size)
let ctx: CGContextRef = UIGraphicsGetCurrentContext()!
CGContextTranslateCTM(ctx,0,-scrollView.frame.origin.y)
UIColor.blackColor().set()
CGContextFillRect(ctx, screenRect)
view.layer.renderInContext(ctx)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()