【问题标题】:Iterating through NSArray works...sometimes遍历 NSArray 工作......有时
【发布时间】:2015-12-07 20:14:48
【问题描述】:

我正在遍历一个 UIImages 数组,它们都需要旋转 90 度。这工作......有时。

我会随机得到一个案例,其中 2 或 3 个图像无法旋转,但我无法始终如一地重现,因此调试很麻烦。

这是我循环数组的方式:

func processPhotosForRotation(completion:() -> Void) {

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {

        for (index,image) in self.frameImages.enumerate() {

            let flippedImage = image.imageRotatedByDegrees(90, flip: false)
            self.frameImages[index] = flippedImage
        }

        //I need the images forwards, then backwards
        self.frameImages.appendContentsOf(self.frameImages.reverse())

        dispatch_async(dispatch_get_main_queue()) {
            completion()
        }
    }
}

这是我旋转图像的方式:

extension UIImage {

    public func imageRotatedByDegrees(degrees: CGFloat, flip: Bool) -> UIImage {

        let degreesToRadians: (CGFloat) -> CGFloat = {
            return $0 / 180.0 * CGFloat(M_PI)
        }

        // calculate the size of the rotated view's containing box for our drawing space
        let rotatedViewBox = UIView(frame: CGRect(origin: CGPointZero, size: size))
        let t = CGAffineTransformMakeRotation(degreesToRadians(degrees));
        rotatedViewBox.transform = t
        let rotatedSize = rotatedViewBox.frame.size

        // Create the bitmap context
        UIGraphicsBeginImageContext(rotatedSize)
        let bitmap = UIGraphicsGetCurrentContext()

        // Move the origin to the middle of the image so we will rotate and scale around the center.
        CGContextTranslateCTM(bitmap, rotatedSize.width / 2.0, rotatedSize.height / 2.0);

        //   // Rotate the image context
        CGContextRotateCTM(bitmap, degreesToRadians(degrees));

        // Now, draw the rotated/scaled image into the context
        var yFlip: CGFloat

        if(flip){
            yFlip = CGFloat(-1.0)
        } else {
            yFlip = CGFloat(1.0)
        }

        CGContextScaleCTM(bitmap, yFlip, -1.0)
        CGContextDrawImage(bitmap, CGRectMake(-size.width / 2, -size.height / 2, size.width, size.height), CGImage)

        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return newImage
    }
}

就像我说的,每次 1-3 次它都可以完美运行,然后时不时地有一些图像不会旋转。

我尝试遍历数组检查image.imageOrientation。即使不是Up,它也会返回相同的结果Up。

【问题讨论】:

  • 您正在从另一个线程修改数组;数组访问不是线程安全的,因此在某些情况下您会丢失对数组的更新。您应该将更新发送到串行队列上的数组(设置新图像的位置)以避免并发更新; stackoverflow.com/questions/24045895/…
  • @Paulw11 这就是问题所在。我尝试了串行队列,但仍然遇到同样的问题。现在我已经开始翻转图像并将它们放入一个临时数组中。然后用临时的替换我原来的。
  • @Paulw11 你能把你的评论移到一个答案,这样我就可以接受:)

标签: ios arrays swift image uiimage


【解决方案1】:

在枚举数组的同时,您正在从多个线程中对数组进行变异。对数组的访问不是线程安全的,因此有时一个线程所做的更改会被另一个线程覆盖。

您应该将更新图像引用存储在临时数组中,并在完成后将其分配给您的属性。这将避免在枚举时修改数组。

我还建议对临时数组的更新在串行队列上同步分派以避免并发更新。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-16
    • 1970-01-01
    • 2021-01-05
    相关资源
    最近更新 更多