私はthis dicussionをフォローしており、元の画像の一部に分割された画像の配列を取得しています。私はそれを印刷した場合、それは次のようになります。1つのイメージを複数の部分に分割した後、SwiftでUIImage配列を使用するにはどうすればよいですか?
[<UIImage: 0x61000008ea60>, {309, 212}, <UIImage: 0x61000008ec90>, {309, 212}, <UIImage: 0x61000008ebf0>, {309, 213}, <UIImage: 0x61000008ec40>, {309, 213}]
は、どのように私は、この配列の要素を使用することができますか?通常の場合、私はイメージの名前を持ち、たとえばUIImage(named: "")
を使用できます。この配列は私に何の名前も表示しません。あなたは何か考えていますか?
おそらく私のミスはここにある:ここで
func setImage(){
testImage = UIImageView(frame: CGRect(x: 0, y: 0, width: size/1.5, height: size/1.5))
testImage.center = CGPoint(x: view.frame.width/2, y: view.frame.height/2)
slice(image: UIImage(named:"leopard_PNG14834")!, into: 8)
testImage.image = images[2]
view.addSubview(testImage)
}
は、すでにタイプUIImage
のオブジェクトの配列を取得する機能コード
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)
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
}
詳細を記入してください。あなたはその画像で何をしたいですか?配列btwのインデックスでアクセスできます。 –
'size'はどこに定義されていますか? –
size = view.frame.width – Roman