我在我的应用程序中使用此代码,这将帮助我发送图像.
但是,我有一个带图像的图像视图.我没有appbundle文件,但我的身边有图像.如何更改以下代码?任何人都可以告诉我如何转换myimage为NSData?
// Attach an image to the email
NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"jpg"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@"rainy"];
Run Code Online (Sandbox Code Playgroud) 我正在尝试保存UIIMage,然后检索并显示它.我已成功将图像保存在捆绑中,从那里检索并显示.我的问题来自于我尝试将图像保存到磁盘(将其转换为NSData),然后检索它.我像这样将图像转换为NSData ...
NSData* topImageData = UIImageJPEGRepresentation(topImage, 1.0);
Run Code Online (Sandbox Code Playgroud)
然后我把它写成磁盘就像这样......
[topImageData writeToFile:topPathToFile atomically:NO];
Run Code Online (Sandbox Code Playgroud)
然后我试着像这样检索它......
topSubView.image = [[UIImage alloc] initWithContentsOfFile:topPathToFile];
Run Code Online (Sandbox Code Playgroud)
它不返回任何图像(大小为零).所以我试过......
NSData *data = [[NSData alloc] initWithContentsOfFile:topPathToFile];
topSubView.image = [[UIImage alloc] initWithData:data];
Run Code Online (Sandbox Code Playgroud)
无济于事.当我单步执行调试器时,我确实看到数据包含正确的字节数,但我很困惑为什么我的图像没有被创建.我错过了一步吗?在转换为图像之前,我是否需要对NSData执行某些操作?