UIimage到NSData返回Null [英] UIimage to NSData returns Null
问题描述
我创建了一个二维码图像并显示在UIImageView中
NSString * QRMessage = @MESSAGE;
NSData * data = [QRMessage dataUsingEncoding:NSISOLatin1StringEncoding allowLossyConversion:false];
CIFilter * filter = [CIFilter filterWithName:@CIQRCodeGenerator];
[filter setValue:data forKey:@inputMessage];
[filter setValue:@QforKey:@inputCorrectionLevel];
imageQRCode = filter.outputImage; // imageQRCode是CIImage
ivQR.image = [UIImage imageWithCIImage:imageQRCode]; // ivQR是UIImageView
我正在尝试保存图像,以便用户可以以某种方式发送QR代码给另一个人。我首先尝试将其保存到剪贴板这样的...
UIPasteboard * pasteBoard = [UIPasteboard pasteboardWithName:UIPasteboardNameGeneral create:没有];
[pasteBoard setPersistent:true];
[pasteBoard setImage:ivQR.image];
...但似乎没有任何东西保存在剪贴板中。
所以我尝试将UIImage转换为NSData,并将其添加为附件:
MFMailComposeViewController * picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
UIImage * imageToSend = [UIImage imageWithCIImage:imageQRCode];
NSData * data = UIImageJPEGRepresentation(imageToSend,1);
[picker addAttachmentData:data mimeType:@image / jpegfileName:@QR.jpg];
但是似乎没有附加任何东西。
我做了一些测试,看起来我从UIImageJPEGRepresentation()中获得的NSData给出了Null数据。该图像其实是在我的手机上显示,所以我想知道我是否只是转换数据错误?
我的大部分谷歌搜索告诉我我正在转换的方式是正确的。但大多数示例使用用户手机上的图片,或者添加到应用程序本身的图片。我的图片是创建的...所以这样做有所作为?
我的目标是允许用户复制到剪贴板或添加为附件的QR图片。
将 CIImage
转换为 UIImage
,我发现, imageWithCIImage
通常不起作用。我通常使用 CIContext
方法 createCGImage
然后创建 UIImage
从那里:
CIImage * ciImage = ...
CIContext * context = [CIContext contextWithOptions:nil];
CGImageRef cgImage = [上下文createCGImage:ciImage fromRect:rect];
UIImage * image = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);
请参阅。/ b / $ b I created a QR code image and displayed it in a UIImageView I'm trying to save the image so the user can somehow send the QR code to another person. I first tried saving it to the "Clipboard" like this... ... but it appears nothing is saved in the Clipboard. So then I tried converting the UIImage to NSData and adding it as an attachment like so: But again nothing seems to be attached. I did some testing and it appears that the NSData I'm getting back from "UIImageJPEGRepresentation()" gives me "Null" data. The image does in fact get displayed on my phone, so I'm wondering if I'm just converting the data wrong? Most of my "googling" tells me that the way I'm converting is correct. But most of the examples use a Picture on the user's phone, or a Picture added to the App itself. My Picture is "created"... so does that make a difference? My goal is to allow the user to Copy to Clipboard, or Add as Attachment of the QR Image. Any assistance is greatly appreciated. When converting a See samples in Core Image Programming Guide. 这篇关于UIimage到NSData返回Null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!NSString *QRMessage = @"MESSAGE";
NSData *data = [QRMessage dataUsingEncoding:NSISOLatin1StringEncoding allowLossyConversion:false];
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[filter setValue:data forKey:@"inputMessage"];
[filter setValue:@"Q" forKey:@"inputCorrectionLevel"];
imageQRCode = filter.outputImage; // imageQRCode is CIImage
ivQR.image = [UIImage imageWithCIImage: imageQRCode]; // ivQR is UIImageView
UIPasteboard *pasteBoard = [UIPasteboard pasteboardWithName:UIPasteboardNameGeneral create:NO];
[pasteBoard setPersistent:true];
[pasteBoard setImage:ivQR.image];
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
UIImage *imageToSend = [UIImage imageWithCIImage:imageQRCode];
NSData *data = UIImageJPEGRepresentation(imageToSend,1);
[picker addAttachmentData:data mimeType:@"image/jpeg" fileName:@"QR.jpg"];
CIImage
to UIImage
, I've found that imageWithCIImage
often doesn't work. I generally use CIContext
method createCGImage
and then create the UIImage
from that:CIImage *ciImage = ...
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgImage = [context createCGImage:ciImage fromRect:rect];
UIImage *image = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);