Я пытаюсь использовать iCarousel в Xcode для отображения изображений.Однако сами изображения не загружаются.
Я реализовал протоколы источника данных, загружаю изображения в массив, а затем использую этот массив, чтобы попытаться заполнить карусель
import UIKit
import CoreImage
class NewPost2ViewController: UIViewController {
struct Filter {
let filterName: String
var filterEffectValue: Any?
var filterEffectValueName: String?
init(filterName: String, filterEffectValue: Any?, filterEffectValueName: String?) {
self.filterName = filterName
self.filterEffectValue = filterEffectValue
self.filterEffectValueName = filterEffectValueName
}
}
//MARK: - UIObjects
@IBOutlet weak var containerView: UIView!
@IBOutlet weak var originalImage: UIImageView!
@IBOutlet weak var imageToFilter: UIImageView!
@IBOutlet weak var carouselView: iCarousel!
//will hold filtered images
var filterImages: [UIImage] = [UIImage]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
loadFilterImages()
print("NUM PHOTOS: \(filterImages.count)")
carouselView.type = .linear
carouselView.reloadData()
}
private func applyFilterTo(image: UIImage, filterEffect: Filter) -> UIImage? {
//get ref to core graphics image
//get ref to graphics context by creating one
guard let cgImage = image.cgImage, let openGLContext = EAGLContext(api: .openGLES3) else {
return nil
}
//create core image context feeding it the graphics context
let context = CIContext(eaglContext: openGLContext)
let ciImage = CIImage(cgImage: cgImage)
let filter = CIFilter(name: filterEffect.filterName)
filter?.setValue(ciImage, forKey: kCIInputImageKey)
//look for any custom filter effects that we may have
if let filterEffectValue = filterEffect.filterEffectValue, let filterEffectValueName = filterEffect.filterEffectValueName {
filter?.setValue(filterEffectValue, forKey: filterEffectValueName)
}
//render the image with a filter
var filteredImage: UIImage?
if let output = filter?.value(forKey: kCIOutputImageKey) as? CIImage, let cgiImageResult = context.createCGImage(output, from: output.extent) {
filteredImage = UIImage(cgImage: cgiImageResult)
}
return filteredImage
}
@IBAction func applySepia(_ sender: Any) {
guard let image = originalImage.image else {
return
}
originalImage.image = applyFilterTo(image: image, filterEffect: Filter(filterName: "CISepiaTone", filterEffectValue: 0.70, filterEffectValueName: kCIInputIntensityKey) )
}
func loadFilterImages() {
for num in 0..<8 {
guard let newImage = UIImage(named: "saladImage") else {
print("ERROR LOADING IMAGE")
return
}
filterImages.append(newImage)
}
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}
//MARK: - Carousel Methods
extension NewPost2ViewController: iCarouselDelegate, iCarouselDataSource {
func numberOfItems(in carousel: iCarousel) -> Int {
print("CAROUSEL COUNT \(filterImages.count)")
return filterImages.count
}
func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {
//Create a UIView
let tempView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
//Create a UIImageView
let frame = CGRect(x: 0, y: 0, width: 100, height: 100)
let imageView = UIImageView()
imageView.frame = frame
imageView.contentMode = .scaleAspectFit
//Set the images of food to the imageView and add it to the temp view
imageView.image = filterImages[index]
tempView.addSubview(imageView)
return tempView
}
}
Я получаю это предупреждение, но не уверен, что оно связано: "Попытка загрузить представление контроллера представления, в то время как это освобождает, не разрешена и может привести к неопределенному поведению"