屏蔽时出现CGAffineTransformMakeRotation错误

时间:2015-10-15 19:33:22

标签: objective-c iphone ipad ios9 cgaffinetransform

屏蔽旋转图像时出现错误。这个错误在iOS 8上没有出现(即使它是使用iOS 9 SDK构建的),并且它仍然不存在于非视网膜iOS 9 iPad(iPad 2)上。我还没有更多仍在iOS 8上的视网膜iPad,与此同时,我还更新了该版本以支持32位和64位架构。重点是,我无法确定iOS 9的更新是否带来了错误,或者是对两种架构设置的更改。

错误的本质就是这个。我正在迭代一系列图像,将它们旋转一个角度,该角度由我想要离开图片的段数确定,并在每次旋转时屏蔽图片以获得图片的不同部分。一切工作正常除了当源图像旋转M_PI,2 * M_PI,3 * M_PI或4 * M_PI次(或多次,即5 * M_PI,6 * M_PI等)时。当它旋转1-3 * M_PI次时,生成的图像被错误地遮盖 - 应该透明的部分最终变黑。当它旋转4 * M_PI次时,结果图像的屏蔽最终会出现nil图像,从而导致应用程序在最后一步中崩溃(我将结果图像添加到数组中)。

我正在使用CIImage进行旋转,并且出于性能原因使用CGImage屏蔽(这种组合表现出最佳性能),所以如果可能的话,我宁愿保留这种方法的选择。

更新:下面显示的代码在后台线程上运行。

以下是代码段:

CIContext *context = [CIContext contextWithOptions:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:kCIContextUseSoftwareRenderer]];

    for (int i=0;i<[src count];i++){
        //for every image in the source array


        CIImage * newImage = [[CIImage alloc] init];

        if  (IS_RETINA){
            newImage = [CIImage imageWithCGImage:[[src objectAtIndex:i] CGImage]];
        }else {
            CIImage *tImage = [CIImage imageWithCGImage:[[src objectAtIndex:i] CGImage]];
            newImage = [tImage imageByApplyingTransform:CGAffineTransformMakeScale(0.5, 0.5)];
        }


        float angle = angleOff*M_PI/180.0;
        //angleOff is just the inital angle offset, If I want to start cutting from a different start point


        for (int j = 0; j < nSegments; j++){
            //for the given number of circle slices (segments)


            CIImage * ciResult = [newImage imageByApplyingTransform:CGAffineTransformMakeRotation(angle + ((2*M_PI/ (2 * nSegments)) + (2*M_PI/nSegments) * (j)))];
            //the way the angle is calculated is specific for the application of the image later. In any case, the bug happens when the resulting angle is M_PI, 2*M_PI, 3*M_PI and 4*M_PI


            CGPoint center = CGPointMake([ciResult extent].origin.x + [ciResult extent].size.width/2, [ciResult extent].origin.y+[ciResult extent].size.width/2);

            for (int k = 0; k<[src count]; k++){
            //this iteration is also specific, it has to do with how much of the     image is being masked in each iteration, but the bug happens irrelevant to this iteration


                CGSize dim = [[masks objectAtIndex:k] size];

                if (IS_RETINA && (floor(NSFoundationVersionNumber)>(NSFoundationVersionNumber_iOS_7_1))) {

                    dim = CGSizeMake(dim.width*2, dim.height*2);

                }//this correction was needed after iOS 7 was introduced, not sure why :)

                CGRect newSize = CGRectMake(center.x-dim.width*1/2,center.y+((circRadius + orbitWidth*(k+1))*scale-dim.height)*1, dim.width*1, dim.height*1);
                //the calculation of the new size is specific to the application, I don't find it relevant.


                CGImageRef imageRef = [context createCGImage:ciResult fromRect:newSize];

                CGImageRef maskRef = [[masks objectAtIndex:k] CGImage];

                CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                                     CGImageGetHeight(maskRef),
                                                     CGImageGetBitsPerComponent(maskRef),
                                                     CGImageGetBitsPerPixel(maskRef),
                                                     CGImageGetBytesPerRow(maskRef),
                                                     CGImageGetDataProvider(maskRef),
                                                     NULL,
                                                     YES);

                CGImageRef masked = CGImageCreateWithMask(imageRef, mask);
                UIImage * result = [UIImage imageWithCGImage:masked];

                CGImageRelease(imageRef);
                CGImageRelease(masked);
                CGImageRelease(maska);
                [temps addObject:result];
            }

        }
    }

我会永远感激任何人可能提供的任何提示。这个错误令我困惑不已。:)

0 个答案:

没有答案