근본적인 문제는 UIImage
과 CGImage
이 어떻게 size
을 해석하는지의 차이입니다. UIImage
은 "점"을 사용하고 CGImage
은 픽셀을 사용합니다. 그리고 전환율은 scale
입니다. UIImage
는 scale
(3)가있는 경우
는 예를 들어, 임의의 주어진 방향 UIImage
모든 "포인트"기본 CGImage
에서 그 방향으로 3 개 개의 화소가있다. 따라서 scale
이 3이고 size
이 100x100 포인트 인 UIImage
의 경우 기본이되는 CGImage
의 크기는 300x300 픽셀입니다.
에 의해 슬라이스 이미지의 간단한 배열을 반환하려면
N X
N, 당신은 스위프트 3에 다음과 같은 작업을 수행 할 수 있습니다 (
N 세의 경우, 배열의 9 개 개의 이미지가있을 것입니다 예) : 스위프트 2.3에
/// Slice image into array of tiles
///
/// - Parameters:
/// - image: The original image.
/// - howMany: How many rows/columns to slice the image up into.
///
/// - Returns: An array of images.
///
/// - Note: The order of the images that are returned will correspond
/// to the `imageOrientation` of the image. If the image's
/// `imageOrientation` is not `.up`, take care interpreting
/// the order in which the tiled images are returned.
func slice(image: UIImage, into howMany: Int) -> [UIImage] {
let width: CGFloat
let height: CGFloat
switch image.imageOrientation {
case .left, .leftMirrored, .right, .rightMirrored:
width = image.size.height
height = image.size.width
default:
width = image.size.width
height = image.size.height
}
let tileWidth = Int(width/CGFloat(howMany))
let tileHeight = Int(height/CGFloat(howMany))
let scale = Int(image.scale)
var images = [UIImage]()
let cgImage = image.cgImage!
var adjustedHeight = tileHeight
var y = 0
for row in 0 ..< howMany {
if row == (howMany - 1) {
adjustedHeight = Int(height) - y
}
var adjustedWidth = tileWidth
var x = 0
for column in 0 ..< howMany {
if column == (howMany - 1) {
adjustedWidth = Int(width) - x
}
let origin = CGPoint(x: x * scale, y: y * scale)
let size = CGSize(width: adjustedWidth * scale, height: adjustedHeight * scale)
let tileCgImage = cgImage.cropping(to: CGRect(origin: origin, size: size))!
images.append(UIImage(cgImage: tileCgImage, scale: image.scale, orientation: image.imageOrientation))
x += tileWidth
}
y += tileHeight
}
return images
}
또는 : 결과 이미지가 올바른 scale
(t에서
이
func slice(image image: UIImage, into howMany: Int) -> [UIImage] {
let width: CGFloat
let height: CGFloat
switch image.imageOrientation {
case .Left, .LeftMirrored, .Right, .RightMirrored:
width = image.size.height
height = image.size.width
default:
width = image.size.width
height = image.size.height
}
let tileWidth = Int(width/CGFloat(howMany))
let tileHeight = Int(height/CGFloat(howMany))
let scale = Int(image.scale)
var images = [UIImage]()
let cgImage = image.CGImage!
var adjustedHeight = tileHeight
var y = 0
for row in 0 ..< howMany {
if row == (howMany - 1) {
adjustedHeight = Int(height) - y
}
var adjustedWidth = tileWidth
var x = 0
for column in 0 ..< howMany {
if column == (howMany - 1) {
adjustedWidth = Int(width) - x
}
let origin = CGPoint(x: x * scale, y: y * scale)
let size = CGSize(width: adjustedWidth * scale, height: adjustedHeight * scale)
let tileCgImage = CGImageCreateWithImageInRect(cgImage, CGRect(origin: origin, size: size))!
images.append(UIImage(CGImage: tileCgImage, scale: image.scale, orientation: image.imageOrientation))
x += tileWidth
}
y += tileHeight
}
return images
}
이 확실하게 있음 그 이유는 위의 "점"의 이미지와 곱하기를 통해 CGImage
의 올바른 픽셀을 얻는 것입니다. 또한 "포인트"로 측정 된 치수)가 n으로 균등하게 나눌 수없는 경우, 해당 행 또는 열의 마지막 이미지에서 차이를 보입니다. 예 : 높이가 736 인 이미지의 타일을 세 개 만들면 처음 두 개는 245 포인트가되지만 마지막 타일은 246 포인트가됩니다.
예외적으로 (완전히) 정상적으로 처리하지 못합니다.즉, UIImage
의 이미지가 .up
이 아닌 imageOrientation
인 경우 이미지를 검색하는 순서는 이미지의 왼쪽 위 모서리가 아니라 해당 방향과 일치합니다.
네, 척도가되어야합니다.하지만 제대로 얻는 것입니다. 어떻게 생각합니까? – JJdip