0

我正在尝试将两张图片合并为一张截图。这是代码:

- (void)savePhoto:(id)sender
   UIImage *snapShotImageGL1 = [glView snapUIImage];
   UIImage *maskedImage = [self maskImage:_image withMask:snapShotImageGL1];
   UIImageWriteToSavedPhotosAlbum(maskedImage, nil, nil, nil);
}

接着:

#pragma mark - Image Processing

- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {

   UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
   [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
   UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();
   return newImage;
}

- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {

CGImageRef maskRef = maskImage.CGImage;

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

CGImageRef masked = CGImageCreateWithMask([image CGImage], mask);
return [UIImage imageWithCGImage:masked];

}

-(UIImage*)invertImage:(UIImage*)image{

    UIGraphicsBeginImageContext(image.size);
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);
    [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeDifference);
    CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(),[UIColor  
    whiteColor].CGColor);
    CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, image.size.width,   
    image.size.height));
    UIImage *returnImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return returnImage;
}

这在模拟器中效果很好,但在我的设备上运行时它只会创建一张白色的空白照片。关于我在这里做错了什么有什么想法吗?

4

1 回答 1

1

检查 Bundle 中的图像名称。因为模拟器不关心文件是否区分大小写,但设备关心。

如果您的图像名称是"ConverImage.png",它将在模拟器中正常工作,但在设备中不能正常工作。尝试将该文件名更改为"convertimage.png".

于 2012-12-04T06:56:51.903 回答