【发布时间】:2012-03-06 16:10:26
【问题描述】:
我在将照片库中的图片附加到电子邮件时遇到了一些问题。
基本上,我的应用程序的一项功能允许用户拍照。当他们拍摄照片时,我会在 Core Data 中记录对图像的 URL 引用。我知道您必须通过ALAssetRepresentation 才能获得图像。当用户想要查看他们拍摄的图像时,我在我的应用程序中启动并运行了它。
我现在正尝试允许用户将所有为活动拍摄的照片附加到电子邮件中。在执行此操作时,我遍历存储 URL 引用的核心数据实体,调用从 ALAssetsLibrary 返回 UIImage 的方法,然后使用 NSData/UIImageJPEGRepresentation 和 MFMailComposeViewController/@ 附加它987654327@ 方法。
问题是:当邮件呈现给用户时,有代表图片的蓝色小方块,图片没有附上。
代码如下:
- (void)sendReportReport
{
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:@"Log: Report"];
NSArray *toRecipients = [NSArray arrayWithObjects:@"someone@someco.com", nil];
[mailer setToRecipients:toRecipients];
NSError *error;
NSFetchRequest *fetchPhotos = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Photo" inManagedObjectContext:__managedObjectContext];
[fetchPhotos setEntity:entity];
NSArray *fetchedPhotos = [__managedObjectContext executeFetchRequest:fetchPhotos error:&error];
int counter;
for (NSManagedObject *managedObject in fetchedPhotos ) {
Photo *photo = (Photo *)managedObject;
// UIImage *myImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png", counter++]];
NSData *imageData = UIImageJPEGRepresentation([self getImage:photo.referenceURL], 0.5);
// NSData *imageData = UIImagePNGRepresentation([self getImage:photo.referenceURL]);
// [mailer addAttachmentData:imageData mimeType:@"image/jpeg" fileName:[NSString stringWithFormat:@"%i", counter]];
[mailer addAttachmentData:imageData mimeType:@"image/jpeg" fileName:[NSString stringWithFormat:@"a.jpg"]];
counter++;
}
NSString *emailBody = [self getEmailBody];
[mailer setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:mailer animated:YES];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure"
message:@"Your device doesn't support the composer sheet"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}
以及返回UIImage的方法:
#pragma mark - Get Photo from Asset Library
+ (ALAssetsLibrary *)defaultAssetsLibrary {
static dispatch_once_t pred = 0;
static ALAssetsLibrary *library = nil;
dispatch_once(&pred, ^{
library = [[ALAssetsLibrary alloc] init];
});
return library;
}
- (UIImage *)getImage:(NSString *)URLReference
{
__block UIImage *xPhoto = nil;
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
UIImage *xImage;
// get the image
ALAssetRepresentation *rep = [myasset defaultRepresentation];
CGImageRef iref = [rep fullScreenImage];
if (iref) {
xImage = [UIImage imageWithCGImage:iref];
}
xPhoto = xImage;
};
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror)
{
NSLog(@"Error fetching photo: %@",[myerror localizedDescription]);
};
NSURL *asseturl = [NSURL URLWithString:URLReference];
// create library and set callbacks
ALAssetsLibrary *al = [DetailsViewController defaultAssetsLibrary];
[al assetForURL:asseturl
resultBlock:resultblock
failureBlock:failureblock];
return xPhoto;
}
注意:上面的代码确实可以运行,它只是不附加图像。另外,请注意,只要我已经将它们设置为UIImageView.Image,我就能够成功地在我的应用程序中附加来自图库的图像(基本上,我正在从UIImageView 中获取指向图像的指针和将它传递给addAttachmentData 方法。)只是当我尝试遍历核心数据并附加而没有先将图像设置为UIImageView 时,我遇到了麻烦。
任何提示将不胜感激!
谢谢! 杰森
【问题讨论】:
-
您是否在将图像添加为附件之前打印了图像的内容? (
CFShow([self getImage:photo.referenceURL]);) 也许现在为零? -
嗨 Jaydee,我猜你是对的。很可能为零。但是,我该怎么办?我是否以错误的方式处理这个问题,但是创建了一个返回 UIImage 的方法?我应该做一些不同的事情来遍历我对照片库图像的 URL 引用列表吗?
-
上去找错误。如果图像为零,那么您的参考 URL 显然是错误的。因此,请检查 URL 是如何创建的..
-
参考网址没有错。正如我在原始帖子中所述,我能够在我的应用程序的另一个视图中成功地从参考 URL 呈现图像。正如您所猜测的那样,图像为零,但我不明白为什么。我认为这与通过返回方法返回 UIImage 有关。如果您了解 ALAssetsLibraryAssetForURLResultBlock,如果您查看 getImage 方法并让我知道您是否看到任何会导致图像返回为 nil 的内容,我将不胜感激。我确实看到了 cfshow 的图像,但它没有返回。
标签: iphone ios ios5 alassetslibrary messageui