フォトギャラリーの画像をメールに添付するのに少し問題があります。
基本的に、私のアプリケーションの機能の1つは、ユーザーが写真を撮ることを可能にします。彼らがショットを撮ったとき、私は画像へのURL参照をCoreDataに記録します。ALAssetRepresentation
画像を取得するには、を通過する必要があることを理解しています。ユーザーが撮影した画像を確認したい場合に備えて、これをアプリケーション内で稼働させています。
現在、イベント用に撮影したすべての写真をユーザーがメールに添付できるようにしようとしています。これを実行している間、URL参照を格納するCore Dataエンティティを反復処理し、からを返すメソッドを呼び出してUIImage
から、 /および/メソッドALAssetsLibrary
を使用してそれをアタッチします。NSData
UIImageJPEGRepresentation
MFMailComposeViewController
addAttachmentData
問題は次のとおりです。電子メールがユーザーに提示されると、画像を表す小さな青い四角があり、画像が添付されていません。
コードは次のとおりです。
- (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
)問題が発生するのは、最初にイメージをに設定せずにCoreDataを反復処理してアタッチしようとしたときですUIImageView
。
ヒントをいただければ幸いです。
ありがとう!ジェイソン