遍历 NSArray 工作......有时

Iterating through NSArray works...sometimes

我正在遍历 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 [=11=] / 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。 returns 结果相同 Up,即使它不是 Up

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

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

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