Я пытаюсь получить основные цвета 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
Получение основных цветов UIImage в Swift ⇐ IOS
Программируем под IOS
1720713163
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..
Подробнее здесь: [url]https://stackoverflow.com/questions/41552544/getting-main-colors-of-a-uiimage-in-swift[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия