0

我正在开发一个应用程序,我正在实现一个应用程序来管理客户的一些订单。在这个视图中,我实现了一个 facebook 样式菜单(通过向右移动整个窗口出现的新菜单),并且当它向右移动时,我在主视图中添加了灰度效果。我通过创建当前屏幕的 UIImage 并将其添加到真实视图并将其 alpha 从 1 设置为 0 来实现它这是我曾经使用过的代码

-(void)toggleMenu {
    UIGraphicsBeginImageContext(self.view.bounds.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    if (![menu isOpened]){
        UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        UIImage *blackAndWhiteImage = [UIImage getBlackAndWhiteVersionOfImage:viewImage];
        overlayImage = [[UIImageView alloc]initWithImage:blackAndWhiteImage];
        overlayImage.alpha = 0.1;
        overlayImage.userInteractionEnabled=NO;
        [self.view addSubview:overlayImage];
    }

    [UIView animateWithDuration:0.3 animations:^{
        CGRect newRect = [self tabBarController].view.frame;
        if (![menu isOpened]){
            newRect.origin.x += 150;
            [menu setOpened:YES];
            overlayImage.alpha = 1.0;
        } else {
            newRect.origin.x -= 150;
            [menu setOpened:NO];
            overlayImage.alpha = 0;
        }
        [self tabBarController].view.frame = newRect;

    } completion:^(BOOL finished){
        if(![menu isOpened]){
            [overlayImage removeFromSuperview];
            overlayImage = nil;
        }
    }];
}

问题是我在动画过程中遇到了性能问题(iPhone 4 很少我在接下来的几个小时内尝试 3gs ......)

有没有人对如何获得更好的表现有任何建议?问候

+ (UIImage *)getBlackAndWhiteVersionOfImage:(UIImage *)anImage {
UIImage *newImage;

if (anImage) {
    CGColorSpaceRef colorSapce = CGColorSpaceCreateDeviceGray();
    CGContextRef context = CGBitmapContextCreate(nil, anImage.size.width * anImage.scale, anImage.size.height * anImage.scale, 8, anImage.size.width * anImage.scale, colorSapce, kCGImageAlphaNone);
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
    CGContextSetShouldAntialias(context, NO);
    CGContextDrawImage(context, CGRectMake(0, 0, anImage.size.width, anImage.size.height), [anImage CGImage]);

    CGImageRef bwImage = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSapce);

    UIImage *resultImage = [UIImage imageWithCGImage:bwImage];
    CGImageRelease(bwImage);

    UIGraphicsBeginImageContextWithOptions(anImage.size, NO, anImage.scale);
    [resultImage drawInRect:CGRectMake(0.0, 0.0, anImage.size.width, anImage.size.height)];
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

return newImage;

}

4

1 回答 1

1

制作屏幕图像的成本很高。我不清楚你想要什么效果,但我会覆盖另一个视图或者动画主视图的移动。

例如,您可以覆盖具有黑色背景但 alpha 为 0.1 的 UIView 以使区域变灰。

于 2011-11-23T14:47:49.467 回答