从ALASSET获取UIImage以在发送电子邮件时使用广告附件

时间:2013-11-26 16:03:41

标签: ios iphone email uiimage alassetslibrary

我有一个包含ALASSETS的集合视图,我希望能够在集合视图中选择一个项目(照片)并将其作为附件发送到电子邮件中。 集合视图基于教程,iPhone编程教程:创建图像库这是最近的帖子,因此它应该是最新的。

用户选择照片时执行的代码如下。

-(void) collectionView:(UICollectionView *)collectionView         didSelectItemAtIndexPath:`enter code here`(NSIndexPath *)indexPath
{ 
 ALAsset *asset = self.assets[indexPath.row];
 ALAssetRepresentation *defaultRep = [asset defaultRepresentation];
 UIImage *image = [UIImage imageWithCGImage:[defaultRep fullScreenImage] scale:[defaultRep scale] orientation:0];



[self.testImage setImage:[UIImage imageWithCGImage:[defaultRep fullScreenImage] scale:[defaultRep scale] orientation:0]];
[self.testImage2 setImage:[UIImage imageWithCGImage:[asset thumbnail]]];

// send email
if ([MFMailComposeViewController canSendMail]) {

    MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
    controller.mailComposeDelegate = self;
    [controller setToRecipients:[NSArray arrayWithObject:@"rasmus4200@gmail.com"]];
    [controller setSubject:@"Subject Goes Here."];
    [controller setMessageBody:@"Your message goes here." isHTML:NO];

    // Attach an image to the email
    //NSData *myData = UIImagePNGRepresentation(coolImage);
    //NSData *myData = UIImageJPEGRepresentation([UIImage imageWithCGImage:[asset thumbnail]], 1.0);
    NSData *myData = UIImageJPEGRepresentation(image, 1.0);
    [controller addAttachmentData:myData mimeType:@"image/jpg" fileName:@"coolImage.jpg"];

    [self presentModalViewController:controller animated:YES];
}
else {
    NSLog(@"Device is unable to send email in its current state.");
}

重要的代码行似乎是。

我现在应该有一张图片,我可以将其作为附件传递给电子邮件,如下所示

NSData *myData = UIImageJPEGRepresentation(image, 1.0);

但是当我查看我的电子邮件时,没有图像。

我想也许我实际上并没有获得资产所以我做了以下测试 我决定将资产缩略图包括如下,现在已注释

NSData *myData = UIImageJPEGRepresentation([UIImage imageWithCGImage:[asset thumbnail]], 1.0);

这样可以正常创建带缩略图的电子邮件

这个问题让我撕掉了我的头发,所以任何帮助都会被感激地接受。

我已经尝试了以下许多形状和形式来检索图像,但似乎没有任何效果

尝试1:

// Set up Blocks
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *asset)
{
    UIImage *coolImage;

    // get the image
    ALAssetRepresentation *rep = [asset defaultRepresentation];
    CGImageRef iref = [rep fullResolutionImage];
    if (iref) {
        coolImage = [UIImage imageWithCGImage:iref];
    }

    // send email
    if ([MFMailComposeViewController canSendMail]) {

        MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
        controller.mailComposeDelegate = self;
        [controller setToRecipients:[NSArray arrayWithObject:@"rasmus4200@gmail.com"]];
        [controller setSubject:@"Subject Goes Here."];
        [controller setMessageBody:@"Your message goes here." isHTML:NO];

        // Attach an image to the email
        //NSData *myData = UIImagePNGRepresentation(coolImage);
        NSData *myData = UIImageJPEGRepresentation(coolImage, 1.0);
        [controller addAttachmentData:myData mimeType:@"image/jpg" fileName:@"coolImage.jpg"];

        [self presentModalViewController:controller animated:YES];
    }
    else {
        NSLog(@"Device is unable to send email in its current state.");
    }
};


ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
{
    NSLog(@"booya, cant get image - %@",[myerror localizedDescription]);
};



// GET THE URL of image
NSURL *url = [[NSURL alloc] init];
url = asset.defaultRepresentation.url;

// create library and set callbacks
ALAssetsLibrary *al = [ConDetailsViewController defaultAssetsLibrary];
[al assetForURL:url
    resultBlock:resultblock
   failureBlock:failureblock];

}

0 个答案:

没有答案