gpt4 book ai didi

ios - asset imageURL 作为字符串传递给另一个类

转载 作者:行者123 更新时间:2023-11-29 02:45:07 26 4
gpt4 key购买 nike

这是我拥有的一个应用程序的简化版本,它从 iPhone/iPad 图像库获取图像 url,将该 URL 的字符串存储在核心数据中,然后在另一个 Controller 中显示它。我认为正确的 URl 存储在核心数据中,但在以后检索时不显示。

为了尝试排除故障,我有一个应用程序可以从 iPhone/iPad 图片库获取图片并将其显示在屏幕上。然后我按下一个按钮以转到第二个 View Controller 并再次显示它。它不会出现在第二个 View Controller 上。我在第一个场景中使用与在第二个 Controller 中使用的方法相同的方法来显示它,只是为了表明该方法可以提供正确的 URL。我将 URL 作为字符串在 Controller 之间传递以模拟存储在核心数据中的 URL,因此将它作为 NSURL 传递不是此示例的选项。

首先查看controller.h和.m文件

//
// patViewController.h
// pictureAssetTest
//
// Created by Chip Cox on 8/10/14.
// Copyright (c) 2014 Home. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "CCExtras.h"
#import <AssetsLibrary/AssetsLibrary.h>

@interface patViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@end



//
// patViewController.m
// pictureAssetTest
//
// Created by Chip Cox on 8/10/14.
// Copyright (c) 2014 Home. All rights reserved.
//

#import "patViewController.h"
#import "patSecondVC.h"

@interface patViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageControl;
@property (nonatomic,strong) NSURL *imageURL;
@end

@implementation patViewController

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

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

- (IBAction)selectPictureFromLibrary:(UIButton *)sender
{
[self startPhotoLibraryFromViewController:self usingDelegate:self];

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES completion:nil];
self.imageURL=[info objectForKey:UIImagePickerControllerReferenceURL];
CCLog(@"Image =%@",[info objectForKey:UIImagePickerControllerReferenceURL]);

[self displayImageFromURL:self.imageURL];
}

- (void) displayImageFromURL:(NSURL*)urlIn
{
ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];

switch(status){
case ALAuthorizationStatusDenied: {
CCLog(@"not authorized");
break;
}
case ALAuthorizationStatusRestricted: {
CCLog(@"Restricted");
break;
}
case ALAuthorizationStatusNotDetermined: {
CCLog(@"Undetermined");
break;
}
case ALAuthorizationStatusAuthorized: {
CCLog(@"Authorized");
CCLog(@"urlIn=%@",urlIn.pathComponents);
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
__block UIImage *returnValue = nil;
[library assetForURL:urlIn resultBlock:^(ALAsset *asset) {
returnValue = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]];
dispatch_async(dispatch_get_main_queue(), ^{
[self.imageControl setImage:returnValue];
[self.imageControl setNeedsDisplay];
});
} failureBlock:^(NSError *error) {
NSLog(@"error : %@", error);
}];
break;
}
default: {
CCLog(@"Unknown hit default");
break;
}
}
}

- (BOOL) startPhotoLibraryFromViewController: (UIViewController*) controller
usingDelegate: (id <UIImagePickerControllerDelegate,
UINavigationControllerDelegate>) delegate {

UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

// Displays a control that allows the user to choose picture or
// movie capture, if both are available:
cameraUI.mediaTypes =
[UIImagePickerController availableMediaTypesForSourceType:
UIImagePickerControllerSourceTypePhotoLibrary];

// Hides the controls for moving & scaling pictures, or for
// trimming movies. To instead show the controls, use YES.
cameraUI.allowsEditing = NO;

cameraUI.delegate = delegate;

[controller presentViewController:cameraUI animated:YES completion:nil];
return YES;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.destinationViewController isKindOfClass:[patSecondVC class]]) {
CCLog(@"Preparing for segue to %@",segue.destinationViewController);
patSecondVC *dvc=segue.destinationViewController;
dvc.transferURL=self.imageURL.absoluteString;
} else {
CCLog(@"Preparing to segue to unknown destination view controller %@",segue.destinationViewController);
}
}

@end

下面的代码是第二个view controller的.h和.m文件。

//
// patSecondVC.h
// pictureAssetTest
//
// Created by Chip Cox on 8/10/14.
// Copyright (c) 2014 Home. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import "CCExtras.h"

@interface patSecondVC : UIViewController
@property (nonatomic,strong) NSString *transferURL;
@end



//
// patSecondVC.m
// pictureAssetTest
//
// Created by Chip Cox on 8/10/14.
// Copyright (c) 2014 Home. All rights reserved.
//

#import "patSecondVC.h"

@interface patSecondVC ()
@property (weak, nonatomic) IBOutlet UIImageView *imageControl;

@end

@implementation patSecondVC

- (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.
CCLog(@"photo url=%@",self.transferURL);
[self displayImageFromURL:[NSURL fileURLWithPath:self.transferURL]];
}

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

- (void) displayImageFromURL:(NSURL*)urlIn
{
ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];

switch(status){
case ALAuthorizationStatusDenied: {
CCLog(@"not authorized");
break;
}
case ALAuthorizationStatusRestricted: {
CCLog(@"Restricted");
break;
}
case ALAuthorizationStatusNotDetermined: {
CCLog(@"Undetermined");
break;
}
case ALAuthorizationStatusAuthorized: {
CCLog(@"Authorized");
CCLog(@"urlIn=%@",urlIn.pathComponents);
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
__block UIImage *returnValue = nil;
[library assetForURL:urlIn resultBlock:^(ALAsset *asset) {
returnValue = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]];
dispatch_async(dispatch_get_main_queue(), ^{
[self.imageControl setImage:returnValue];
[self.imageControl setNeedsDisplay];
});
} failureBlock:^(NSError *error) {
NSLog(@"error : %@", error);
}];
break;
}
default: {
CCLog(@"Unknown hit default");
break;
}

}

}
@end

谢谢

添加了日志输出

2014-08-11 17:05:37.194 pictureAssetTest[2544:60b] <-[patViewController imagePickerController:didFinishPickingMediaWithInfo:] - 45> Image =assets-library://asset/asset.PNG?id=5E208B2D-65CC-4F46-8F22-DD2E8006AF1B&ext=PNG
2014-08-11 17:05:37.195 pictureAssetTest[2544:60b] <-[patViewController displayImageFromURL:] - 68> Authorized
2014-08-11 17:05:37.196 pictureAssetTest[2544:60b] <-[patViewController displayImageFromURL:] - 69> urlIn=(
"/",
"asset.PNG"
)
2014-08-11 17:05:39.149 pictureAssetTest[2544:60b] <-[patViewController prepareForSegue:sender:] - 116> Preparing for segue to <patSecondVC: 0xa49c610>
2014-08-11 17:05:39.150 pictureAssetTest[2544:60b] <-[patSecondVC viewDidLoad] - 31> photo url=assets-library://asset/asset.PNG?id=5E208B2D-65CC-4F46-8F22-DD2E8006AF1B&ext=PNG
2014-08-11 17:05:39.151 pictureAssetTest[2544:60b] <-[patSecondVC displayImageFromURL:] - 59> Authorized
2014-08-11 17:05:39.151 pictureAssetTest[2544:60b] <-[patSecondVC displayImageFromURL:] - 60> urlIn=(
"/",
"assets-library:",
asset,
"asset.PNG?id=5E208B2D-65CC-4F46-8F22-DD2E8006AF1B&ext=PNG"
)

最佳答案

在第二个 vc 中将调用更改为

[self displayImageFromURL:[NSURL URLWithString:self.transferURL]];

这应该可以解决问题!

关于ios - asset imageURL 作为字符串传递给另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25234968/

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