gpt4 book ai didi

ios - 我如何在 iOS 中获取捕获的图像路径?

转载 作者:行者123 更新时间:2023-11-29 03:22:41 25 4
gpt4 key购买 nike

我正在尝试使用 iphone 捕捉图像并将其保存在相册中。我可以在那里看到我的图像,但无法获得图像的确切路径?这是我为此使用的代码。

#import "captureViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface captureViewController ()
{
UIImageView *imageview;
NSString *str;
UIImagePickerController *picker;
}
@property (strong, nonatomic) IBOutlet UIImageView *imageview;
@end

@implementation captureViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)captureButton:(id)sender {
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;

[self presentViewController:picker animated:YES completion:NULL];
}

- (IBAction)selectButton:(id)sender {
}

- (IBAction)selectPhotos
{
picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
[self presentModalViewController:picker animated:YES];
// [picker release];
}

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
imageview.image = image;
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker
{
[picker dismissModalViewControllerAnimated:YES];
}

- (void)pickerDone:(id)sender
{
[picker dismissModalViewControllerAnimated:YES];

}


- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

[library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
if (error) {
NSLog(@"error");
} else {
NSLog(@"url %@", assetURL);
[self getimage:assetURL];
}

}];

[picker dismissModalViewControllerAnimated:YES];

}
-(void)getimage:(NSURL *)url{
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
ALAssetRepresentation *rep = [myasset defaultRepresentation];
CGImageRef iref = [rep fullResolutionImage];
if (iref) {
UIImage *largeimage = [UIImage imageWithCGImage:iref];
// update image
imageview.image = largeimage;
} else {
NSLog(@"Error in resultblock in PhotoAlbumViewController!");
}
};

// failure block
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror)
{
NSLog(@"Can't get image - %@",[myerror localizedDescription]);
};

ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
str=[NSString stringWithFormat:@"%@",url];
[assetslibrary assetForURL:url
resultBlock:resultblock
failureBlock:failureblock];
}

我在这里使用 ALAssetsLibrary 但我得到的路径如下格式

 assets-library://asset/asset.JPG?id=6BD5603D-BB7D-46E3-84A3-43BB33C735BF&ext=JPG

我试图在同一页面的一个 UIImageView 中加载此图像,所以当我提供此路径时图像未加载(检查 getImage ())

邮件图片附加代码

    - (IBAction)selectButton:(id)sender
{

if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];

mailer.mailComposeDelegate = self;

[mailer setSubject:@"eGift"];

NSArray *toRecipients = [NSArray arrayWithObjects:@"", nil];
[mailer setToRecipients:toRecipients];
NSLog(@"%@",str);//Is the asset url
UIImage *myImage = [UIImage imageWithContentsOfFile:str];
NSData *imageData = UIImagePNGRepresentation(myImage);
[mailer addAttachmentData:imageData mimeType:@"image/png" fileName:str];

NSString *emailBody = @"Please check the attached image: Naveen";
[mailer setMessageBody:emailBody isHTML:NO];

// only for iPad
// mailer.modalPresentationStyle = UIModalPresentationPageSheet;

[self presentModalViewController:mailer animated:YES];

//[mailer release];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure"
message:@"Your device doesn't support the composer sheet"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
// [alert release];
}

}



#pragma mark - MFMailComposeController delegate


- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(@"Mail cancelled: you cancelled the operation and no email message was queued");
break;
case MFMailComposeResultSaved:
NSLog(@"Mail saved: you saved the email message in the Drafts folder");
break;
case MFMailComposeResultSent:
NSLog(@"Mail send: the email message is queued in the outbox. It is ready to send the next time the user connects to email");
break;
case MFMailComposeResultFailed:
NSLog(@"Mail failed: the email message was nog saved or queued, possibly due to an error");
break;
default:
NSLog(@"Mail not sent");
break;
}

[self dismissModalViewControllerAnimated:YES];
}

最佳答案

这是通过 ALAssetsLibrary 正确加载图像的方式:

/* load photo from directory */

// result block
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
ALAssetRepresentation *rep = [myasset defaultRepresentation];
CGImageRef iref = [rep fullResolutionImage];
if (iref) {
UIImage *largeimage = [UIImage imageWithCGImage:iref];
// update image
imageview.image = largeimage;
} else {
NSLog(@"Error in resultblock in PhotoAlbumViewController!");
}
};

// failure block
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror)
{
NSLog(@"Can't get image - %@",[myerror localizedDescription]);
};

ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];

[assetslibrary assetForURL:url
resultBlock:resultblock
failureBlock:failureblock];

关于ios - 我如何在 iOS 中获取捕获的图像路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20885509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com