制作从 google(google 个地方,swift)下载的圆形照片
Make circular photos downloaded from google (google places, swift)
我在 google 个地方工作,我有一个 VC 和一个 tableView,我从用户位置下载附近的地方,并在 tableView 的每个单元格中添加信息地方和它的照片。我创建了一个自定义 class 使照片成为圆形
import UIKit
@IBDesignable
class RoundImage: UIImageView {
@IBInspectable var cornerRadius: CGFloat = 0 {
didSet {
self.layer.cornerRadius = cornerRadius
}
}
@IBInspectable var borderWidth: CGFloat = 0 {
didSet {
self.layer.borderWidth = borderWidth
}
}
@IBInspectable var borderColor: UIColor = UIColor.clear {
didSet {
self.layer.borderColor = borderColor.cgColor
}
}
}
但问题是我用这个 class
下载了这些地方的照片
import UIKit
private let widthKey = "width"
private let heightKey = "height"
private let photoReferenceKey = "photo_reference"
class QPhoto: NSObject {
var width: Int?
var height: Int?
var photoRef: String?
init(photoInfo: [String:Any]) {
height = photoInfo[heightKey] as? Int
width = photoInfo[widthKey] as? Int
photoRef = photoInfo[photoReferenceKey] as? String
}
func getPhotoURL(maxWidth:Int) -> URL? {
if let ref = self.photoRef {
return NearbyPlaces.googlePhotoURL(photoReference: ref, maxWidth: maxWidth)
}
return nil
}
}
我想知道如何调整它,因为有了这个 class 我下载的照片,即使我将 RoundImage
class 放在图像视图中故事板总是方形的
您需要将 clipsToBounds
设置为 true
。您可以在设置 cornerRadius
:
时执行此操作
@IBInspectable var cornerRadius: CGFloat = 0 {
didSet {
self.layer.cornerRadius = cornerRadius
self.clipsToBounds = true
}
}
我在 google 个地方工作,我有一个 VC 和一个 tableView,我从用户位置下载附近的地方,并在 tableView 的每个单元格中添加信息地方和它的照片。我创建了一个自定义 class 使照片成为圆形
import UIKit
@IBDesignable
class RoundImage: UIImageView {
@IBInspectable var cornerRadius: CGFloat = 0 {
didSet {
self.layer.cornerRadius = cornerRadius
}
}
@IBInspectable var borderWidth: CGFloat = 0 {
didSet {
self.layer.borderWidth = borderWidth
}
}
@IBInspectable var borderColor: UIColor = UIColor.clear {
didSet {
self.layer.borderColor = borderColor.cgColor
}
}
}
但问题是我用这个 class
下载了这些地方的照片import UIKit
private let widthKey = "width"
private let heightKey = "height"
private let photoReferenceKey = "photo_reference"
class QPhoto: NSObject {
var width: Int?
var height: Int?
var photoRef: String?
init(photoInfo: [String:Any]) {
height = photoInfo[heightKey] as? Int
width = photoInfo[widthKey] as? Int
photoRef = photoInfo[photoReferenceKey] as? String
}
func getPhotoURL(maxWidth:Int) -> URL? {
if let ref = self.photoRef {
return NearbyPlaces.googlePhotoURL(photoReference: ref, maxWidth: maxWidth)
}
return nil
}
}
我想知道如何调整它,因为有了这个 class 我下载的照片,即使我将 RoundImage
class 放在图像视图中故事板总是方形的
您需要将 clipsToBounds
设置为 true
。您可以在设置 cornerRadius
:
@IBInspectable var cornerRadius: CGFloat = 0 {
didSet {
self.layer.cornerRadius = cornerRadius
self.clipsToBounds = true
}
}