xcode iOS 6 MFMailComposeViewController: 只支持 RGBA 或 White 颜色空间,这个方法是一个 hack
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12593299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS 6 MFMailComposeViewController: Only support RGBA or the White color space, this method is a hack
提问by James Gupta
really, really odd error
真的,非常奇怪的错误
I have an app that was working fine in iOS5/.1, now I'm having a few transition problems with iOS6 but this one is confusing.
我有一个在 iOS5/.1 中运行良好的应用程序,现在我在 iOS6 中遇到了一些转换问题,但这个问题令人困惑。
I have some code that launches a mail composer, and since iOS 6 it causes a crash with this error:
我有一些代码可以启动邮件编辑器,从 iOS 6 开始,它会导致崩溃并出现此错误:
* Assertion failure in -[UICGColor encodeWithCoder:], /SourceCache/UIKit/UIKit-2372/UIColor.m:1191 2012-09-26 02:14:38.044 MyCQs Medical[2126:1b03] *Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Only support RGBA or the White color space, this method is a hack.'
* 断言失败 -[UICGColor encodeWithCoder:], /SourceCache/UIKit/UIKit-2372/UIColor.m:1191 2012-09-26 02:14:38.044 MyCQs Medical[2126:1b03] *由于未捕获的异常而终止应用程序' NSInternalInconsistencyException',原因:'仅支持 RGBA 或白色颜色空间,此方法是一种 hack。
Any suggestions? Through trial and error commenting out various lines it seems to be the alloc/init line that causes the error, though when all the lines are uncommented, all of the NSLogs are executed, including "present" which indicates that everything that should be called, has been. the app crashes before the mailcomposer is presented on screen, I'd really appreciate any advice here
有什么建议?通过反复试验注释掉各个行,似乎是 alloc/init 行导致了错误,尽管当所有行都被取消注释时,所有 NSLog 都会被执行,包括“present”,它表示应该调用的所有内容,一直。应用程序在 mailcomposer 出现在屏幕上之前崩溃,我真的很感激这里的任何建议
if (indexPath.row == 3) {
if([MFMailComposeViewController canSendMail]){
mailComposer = [[MFMailComposeViewController alloc]init];
NSLog(@"Alloc, init");
mailComposer.mailComposeDelegate = self;
NSLog(@"Set delegate");
NSArray *toArray = [[NSArray alloc]initWithObjects:@"[email protected]", nil];
NSLog(@"To array");
[mailComposer setToRecipients:toArray];
NSLog(@"To recipients");
[mailComposer setSubject:@"Message from a MyCQs user!"];
NSLog(@"Subject");
NSLog(@"About to present mail composer");
[[mailComposer navigationBar] setTintColor:[UIColor blackColor]];
[self presentModalViewController:mailComposer animated:YES];
NSLog(@"Present");
}
}
回答by James Gupta
Ok only a partial solution but it's the best one I can think of without some fairly disruptive code changes.
好吧,这只是部分解决方案,但它是我能想到的最好的解决方案,没有一些相当具有破坏性的代码更改。
For anyone else who gets this problem in future, I think it's a bug in iOS 6, MFMailComposeViewController crashes when you have set the UITableView separatorStyle to a colorWithPatternImage, but it works fine when you use a solid colour, so:
对于将来遇到此问题的任何其他人,我认为这是 iOS 6 中的一个错误,当您将 UITableView separatorStyle 设置为 colorWithPatternImage 时,MFMailComposeViewController 会崩溃,但是当您使用纯色时它可以正常工作,因此:
if ([[[UIDevice currentDevice] systemVersion] floatValue] > 5.2) {
NSLog(@"Using iOS 5x");
[[UITableView appearance]setSeparatorColor:[UIColor colorWithRed:142.0/255.0 green:130.0/255.0 blue:76.0/255.0 alpha:1.0]];
}
else {
NSLog(@"Using iOS 6x, Table view use pattern color");
[[UITableView appearance]setSeparatorColor:[UIColor colorWithPatternImage: [UIImage imageNamed:@"dotted-line2.png"]]];
}
回答by coolio
Actually I believe this is a significant bug, did anybody file a radar, yet?
实际上,我认为这是一个重大错误,有人提交过雷达吗?
As it seems to me, the assertion throws whenever you use "colorWithPatternImage" ANYWHERE in your presenting view controller's appearance proxies.
在我看来,只要您在呈现视图控制器的外观代理中使用“colorWithPatternImage”任何地方,就会抛出断言。
I think what happens is that iOS tries to store the appearance of your App before switching to a separate service (which is what MFMailComposeViewController does, it's now a "remote view controller" that gets presented by your App but which is managed by another App/process), since the Mail App wants to determine the appearance itself so it changes things like tint colors etc. More about remote view controller here, in case somebody is interested: http://oleb.net/blog/2012/10/remote-view-controllers-in-ios-6/
我认为发生的情况是 iOS 在切换到单独的服务之前尝试存储您的应用程序的外观(这就是 MFMailComposeViewController 所做的,它现在是一个“远程视图控制器”,由您的应用程序呈现,但由另一个应用程序管理/过程),因为邮件应用程序想要确定外观本身,所以它会改变色调等内容。更多关于远程视图控制器的信息,如果有人感兴趣:http: //oleb.net/blog/2012/10/remote -view-controllers-in-ios-6/
This seems to fail. This has to be a bug, the image should be encodable.
这似乎失败了。这必须是一个错误,图像应该是可编码的。
Workarounds may be hard. I tried to exchange the patterned color for a plain one right before presenting the view controller but found you at least have to make sure it really gets redrawn on screen and gave up (I don't really needed the pattern).
解决方法可能很难。我试图在呈现视图控制器之前将带图案的颜色交换为普通颜色,但发现您至少必须确保它真的在屏幕上重新绘制并放弃(我真的不需要这种图案)。
回答by Imran
My problem was also due to using colorWithPatternImage but I found that it seems to only cause an issue when used on elements that are shown on the MFMailCompserViewController e.g.:
我的问题也是由于使用 colorWithPatternImage 但我发现它似乎只在 MFMailCompserViewController 上显示的元素上使用时才会导致问题,例如:
[[UINavigationBar appearance] setTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"navbar"]]];
回答by Steve Weyl
I've found that MFMailComposeViewController only crashes when colorWithPatternImage
is used in [[UITableView appearance]
statement. I've had no problem when I set the background and separatorColor in the table's viewDidLoad. The only problem is you need to do this for each table.
我发现 MFMailComposeViewController 只有colorWithPatternImage
在[[UITableView appearance]
语句中使用时才会崩溃。当我在表的 viewDidLoad 中设置背景和分隔符颜色时,我没有遇到任何问题。唯一的问题是您需要为每个表都执行此操作。
[self.tableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"image.png"]]];
[self.tableView setSeparatorColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"image.png"]]];
回答by taber
@coolio is right, it seems to happen when using a patterned color with any controller class. (At leastUITabBarController and UITableViewController, apparently.)
@coolio 是对的,在任何控制器类中使用图案颜色时似乎都会发生这种情况。(至少UITabBarController 和 UITableViewController,显然。)
I got around this by subclassing my tab bar controller and using appearanceWhenContainedIn:. That way you can still use your fancy texture with no need to subclass MFMailComposeViewController or sniff iOS versions, etc.
我通过子类化我的标签栏控制器并使用 AppearanceWhenContainedIn: 来解决这个问题。这样你仍然可以使用你喜欢的纹理,而无需继承 MFMailComposeViewController 或嗅探 iOS 版本等。
UIColor *tabBarBgPattern = [UIColor colorWithPatternImage:...];
[[UITabBar appearanceWhenContainedIn:[MyTabBarController class],
nil]
setBackgroundColor:tabBarBgPattern];
回答by link82
I'm facing the same issue using 'colorWithPatterImage' for a UIBarButtonItem inside the navBar of my tableViewController used to display the a modal view (I'm not using MFMailComposeViewController).
我在 tableViewController 的导航栏中使用 'colorWithPatterImage' 作为 UIBarButtonItem 面临同样的问题,用于显示模式视图(我没有使用MFMailComposeViewController)。
[[UIBarButtonItem appearance] setTintColor:[UIColor colorWithPatternImage:bgImage]];
I needed to replace it with a RGBa color;
我需要用 RGBa 颜色替换它;
回答by Chip Bancroft
This thread gave me great clues on how to fix a similar problem in my App using ShareKit and attempting to email an image of a view. Now I only change the UISwitch appearance for versions less than 6.0.
该线程为我提供了有关如何使用 ShareKit 修复我的应用程序中的类似问题并尝试通过电子邮件发送视图图像的重要线索。现在我只更改低于 6.0 的版本的 UISwitch 外观。
This code gets called when my app starts up:
当我的应用程序启动时调用此代码:
// Protect against IOS 6.0 BUG: 'NSInternalInconsistencyException', reason: 'Only support RGBA or the White color space, this method is a hack.'
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 6.0) {
UIImage *patternImage = [UIImage imageNamed:@"solid-colors_blue.png"];
UIColor* switchColor = [UIColor colorWithPatternImage:patternImage];
[[UISwitch appearance] setOnTintColor: switchColor];
}
回答by DiscDev
My apps were crashing on iOS 7 when using colorWithPatternImage
and trying to present an MFMailComposeViewController
. I have to determine the color from a UIImage because the UIImage can be chosen by the user, and I want to "theme" my application based on the user's selected image.
我的应用程序在 iOS 7 上使用colorWithPatternImage
并尝试呈现MFMailComposeViewController
. 我必须确定 UIImage 的颜色,因为 UIImage 可以由用户选择,并且我想根据用户选择的图像“主题化”我的应用程序。
The solution for me (albeit kind of hacky) is to grab the RGBA values of the origin (0,0) of the UIImage, then apply that as the background color instead of using colorWithPatternImage
.
我的解决方案(尽管有点笨拙)是获取 UIImage 原点 (0,0) 的 RGBA 值,然后将其用作背景颜色而不是使用colorWithPatternImage
.
+ (UIColor*)getUIColorFromImageAtOrigin:(UIImage*)image
{
// First get the image into your data buffer
CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
// Now your rawData contains the image data in the RGBA8888 pixel format.
int byteIndex = (bytesPerRow * 0) + 0 * bytesPerPixel;
CGFloat red = (rawData[byteIndex] * 1.0) / 255.0;
CGFloat green = (rawData[byteIndex + 1] * 1.0) / 255.0;
CGFloat blue = (rawData[byteIndex + 2] * 1.0) / 255.0;
CGFloat alpha = (rawData[byteIndex + 3] * 1.0) / 255.0;
UIColor *acolor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
free(rawData);
return acolor;
}
Now if I use this UIColor
as the background instead of colorWithPatternImage
, I can share to my heart's content without encountering this weird exception.
现在,如果我使用 thisUIColor
作为背景而不是colorWithPatternImage
,我可以尽情分享,而不会遇到这种奇怪的异常。
The above method was adapted from a solution that I found here: https://stackoverflow.com/a/1262893/1103584
上述方法改编自我在这里找到的解决方案:https: //stackoverflow.com/a/1262893/1103584
回答by Vlad Polyanskiy
It's a bug of iOS 6 i report it and it was marked as dublicate. So, someone report that bug earlier.
这是 iOS 6 的一个错误,我报告了它,它被标记为重复。因此,有人更早地报告了该错误。
回答by John
For using pattern, you can use resizableImageWithCapInsets. In Monotouch it seems like this:
对于使用模式,您可以使用 resizableImageWithCapInsets。在 Monotouch 中,它看起来像这样:
var sba = UISearchBar.Appearance;
var img = UIImage.FromBundle("myResource.png");
if (UIDevice.CurrentDevice.CheckSystemVersion(6, 0)) {
img = img.CreateResizableImage(UIEdgeInsets.Zero, UIImageResizingMode.Tile);
sba.BackgroundColor = UIColor.LightGray;
sba.BackgroundImage = img;
} else {
sba.BackgroundImage = new UIImage();
sba.BackgroundColor = UIColor.FromPatternImage(img);
}