Swift 개발 팁
2020. 5. 20. 14:52ㆍ아이폰 개발
커스텀 View에서 이미지 출력시 뒤집어질 경우 처리 방법
extension CGContext {
/// Draw `image` flipped vertically, positioned and scaled inside `rect`.
public func drawFlipped(_ image: CGImage, in rect: CGRect) {
self.saveGState()
self.translateBy(x: 0, y: rect.origin.y + rect.height)
self.scaleBy(x: 1.0, y: -1.0)
self.draw(image, in: CGRect(origin: CGPoint(x: rect.origin.x, y: 0), size: rect.size))
self.restoreGState()
}
}
ctx.drawFlipped(myImage, in: myRect)
파일명과 확장자 추출하는 방법
extension String {
func lastPathComponent(withExtension: Bool = true) -> String {
let lpc = self.nsString.lastPathComponent
return withExtension ? lpc : lpc.nsString.deletingPathExtension
}
var nsString: NSString {
return NSString(string: self)
}
}
let path = "/very/long/path/to/filename_v123.456.plist"
let filename = path.lastPathComponent(withExtension: false)
UIImage 리사이즈 하는 방법
extension UIImage {
func resized(to size: CGSize) -> UIImage {
return UIGraphicsImageRenderer(size: size).image { _ in
draw(in: CGRect(origin: .zero, size: size))
}
}
}
let resizedImage = image.resized(to: CGSize(width: 50, height: 50))
https://stackoverflow.com/questions/2658738/the-simplest-way-to-resize-an-uiimage
탭바를 프로그램에서 전환하는 방법
func switchToDataTab(){
NSTimer.scheduledTimerWithTimeInterval(0.2, target: self, selector: #selector(switchToDataTabCont), userInfo: nil,repeats: false)
}
func switchToDataTabCont(){
tabBarController!.selectedIndex = 0
}
https://stackoverflow.com/questions/28099148/switch-tab-bar-programmatically-in-swift
컬렉션뷰 구현 방법
https://www.raywenderlich.com/9334-uicollectionview-tutorial-getting-started
https://www.journaldev.com/10678/ios-uicollectionview-example-tutorial