Получение основных цветов UIImage в SwiftIOS

Программируем под IOS
Ответить
Anonymous
 Получение основных цветов UIImage в Swift

Сообщение Anonymous »

Я пытаюсь получить основные цвета UIImage в Swift и попытался перенести этот код. К сожалению, код продолжает возвращать одни и те же цвета. Я видел, что ответ, представленный здесь, также продолжает возвращать тот же цвет. Я избегал использования CIFilter, поскольку в моих исследованиях он возвращает только средний цвет.

Ниже перенесенный код. Я установил для данных CGContext значение nil, так как Swift может обрабатывать память, когда он включен, и во время моего тестирования он выдавал много ошибок памяти.

func mainColors(image:UIImage, detail: Int) -> [UIColor] {
//COLOR PROCESS STEP 1:
//Determine the detail.
var dimension = 10
var flexibility = 2
var range = 60

//Low detail.
if detail == 0 {
dimension = 4
flexibility = 1
range = 100
}
//High detail.
else if detail == 2 {
dimension = 100
flexibility = 10
range = 20
}

//COLOR PROCESS STEP 2:
//Determine the colors in the image.

//Create an array to store the colors.
var colors = Array()

//Get the bitmap data of the image.
let imageRef = image.cgImage
//Variable to store the color space, RGB in this case.
let colorSpace = CGColorSpaceCreateDeviceRGB()
//Additional CGContext data.
let bytesPerPixel = 4
let bytesPerRow = bytesPerPixel * dimension
let bitsPerComponent = 8
//Create the context. Data uses the memory pointer created above, the width and height determine the dimensions of the bitmap, the space is for the colorspace, the bitmap specifies the alpha channel.
let context = CGContext(data: nil, width: dimension, height: dimension, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue | CGBitmapInfo.byteOrder32Big.rawValue)!
//Draw the image.
let rect = CGRect(x: 0, y: 0, width: dimension, height: dimension)
context.draw(imageRef!, in: rect)

//Iterate through the raw data in order to create a UIColor.
var x = 0
var y = 0

for _ in 0..

Подробнее здесь: https://stackoverflow.com/questions/415 ... e-in-swift
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «IOS»