问题描述
我需要在CameraSessionView之间传回照片的NSMutableArray;如何将从相机拍摄的照片存储在NSMutableArray上,而TableViewController会将此照片上传到DropBox。我正在使用委托和协议,但我尝试的所有方式都失败了。
任何人都可以帮助我。我认为我做一些小事情错了。
我向你显示一些代码:
I need to pass back an NSMutableArray of photos between a CameraSessionView; how store the photos taken from camera on an NSMutableArray, and a TableViewController how uploads this photos to DropBox. I'm using delegates and protocols, but all the ways I tried... fail.Anyone can help me. I think Im doing some little thing wrong.I show you some code:
CameraSessionView.h
CameraSessionView.h
@class CameraSessionView;
@protocol CameraSessionViewDelegate <NSObject>
@optional
-(void)uploadPhotosFromCamera:(NSMutableArray*)photos;
@end
@property (nonatomic, weak) id <CameraSessionViewDelegate> delegado;
CameraSessionView.m
CameraSessionView.m
@property (nonatomic, strong) NSMutableArray* images;
- (void)onTapOkButton{
NSLog(@"Save photos");
if([self.delegado respondsToSelector:@selector(uploadPhotosFromCamera:)])
[self.delegado uploadPhotosFromCamera:_images];
[self onTapDismissButton];
}
PhotosTableViewController.h
PhotosTableViewController.h
@interface PhotosTableViewController : UITableViewController <CameraSessionViewDelegate>
PhotosTableViewController.m
PhotosTableViewController.m
@property (nonatomic, strong) CameraSessionView *camera;
- (void)viewDidLoad
{
_camera = [CameraSessionView new];
[_camera setDelegado:self];
}
-(void)uploadPhotosFromCamera:(NSMutableArray*)photos
{
NSLog(@"UPFC");
for(int x=0; x < [photos count];x++)
{
NSLog(@"UPFC...");
UIImage *foto = [photos objectAtIndex:x];
if (foto.size.height > 1000 || foto.size.width > 1000)
foto = [self imageWithImage:foto scaledToScale:0.15f];
DBMetadata* datos = [TablaSubidas addFile:pathElemento];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSData *data = UIImageJPEGRepresentation(foto, 1.0);
[fileManager createFileAtPath:[self photoPath:datos] contents:data attributes:nil];
[elementosTabla insertObject:datos atIndex:0];
}
[self sincFotos];
[self.tableView reloadData];
}
只有当我按OK按钮时,照片才会发回到PhotosTableViewController,将被上传到保管箱。
Only wants that when I press OK button the photos send back to PhotosTableViewController where it would be uploaded to dropbox.
self.delegado onTapOKButton总是没有。
self.delegado on onTapOKButton is always nil.
看起来很容易,但我不能运行。
我非常感谢任何人可以帮助我或推荐我任何教程...
Looks easy but I cant run it.I'm so grateful if anyone could help me or recommend me any tutorial...
谢谢!
推荐答案
一旦viewDidLoad,您的 CameraSessionView
实例将从内存中释放结束。您需要将其存储在 PhotosTableViewController
中的属性中,以便保留。
Your CameraSessionView
instance will be released from memory as soon as viewDidLoad ends. You need to store it in a property in PhotosTableViewController
so that it is retained.
您的代理也应该被定义如弱,例如
Your delegate should also be defined as weak, e.g.
@property (nonatomic,weak) id< CameraSessionViewDelegate >delegado;
然后在执行PhotosTableViewController时,您需要实现 (void)uploadPhotosFromCamera:(NSMutableArray *)照片;
方法。
Then in your implementation of PhotosTableViewController, you'll need to implement the -(void)uploadPhotosFromCamera:(NSMutableArray*)photos;
method.
此外,由于此方法定义为 @optional
,您应该检查代理在调用它之前是否响应它。
Also as this method is defined as @optional
, you should check if the delegate responds to it before calling it.
if([self.delegado respondsToSelector:@selector(uploadPhotosFromCamera:]){
[self.delegado uploadPhotosFromCamera:_images];
}
这将阻止应用程序崩溃如果委托方法是不执行。
This will prevent the app from crashing if the delegate method isn't implemented.
这篇关于在ObjectiveC上的代表之间传回信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!