UIImageView+Alpha.swift 802 B

123456789101112131415161718192021222324252627282930313233343536
  1. //
  2. // UIImageViewExtenstions.swift
  3. // Photo Editor
  4. //
  5. // Created by Mohamed Hamed on 5/10/17.
  6. //
  7. //
  8. import Foundation
  9. import UIKit
  10. extension UIImageView {
  11. func alphaAtPoint(_ point: CGPoint) -> CGFloat {
  12. var pixel: [UInt8] = [0, 0, 0, 0]
  13. let colorSpace = CGColorSpaceCreateDeviceRGB();
  14. let alphaInfo = CGImageAlphaInfo.premultipliedLast.rawValue
  15. guard let context = CGContext(data: &pixel, width: 1, height: 1, bitsPerComponent: 8, bytesPerRow: 4, space: colorSpace, bitmapInfo: alphaInfo) else {
  16. return 0
  17. }
  18. context.translateBy(x: -point.x, y: -point.y);
  19. layer.render(in: context)
  20. let floatAlpha = CGFloat(pixel[3])
  21. return floatAlpha
  22. }
  23. }