我在BUTTON点击时打开了图库并选择了一个图像。我保存图像的完整路径 在string.Now如何在按钮中保存该字符串。我必须将字符串保存到按钮中,以便按钮可以保存路径。我在视图中有如此多的按钮,并且必须执行相同的操作。代码我用于保存图像路径写在下面。
//从结果中获取图像
UIImage* image = [info valueForKey:@"UIImagePickerControllerOriginalImage"];
// Get the data for the image as a PNG
NSData* imageData = UIImagePNGRepresentation(image);
// Give a name to the file
NSString * imageName = @"Myimage.png";
// Now, we have to find the documents directory so we can save it
// Note that you might want to save it elsewhere, like the cache directory,
// or something similar.
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
// Now we get the full path to the file
NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
// and then we write it out
[imageData writeToFile:fullPathToFile atomically:NO];
Button = fullPathToFile; ///This is the button
return;
答案 0 :(得分:0)
这里是向UIButton等对象添加属性的解决方案。
的UIButton + STRING.H
#import <Foundation/Foundation.h>
@interface UIButton (String)
@property (nonatomic, retain) NSString *path;
@end
的UIButton + String.m
#import "UIButton+String.h"
#import <objc/runtime.h>
@implementation UIButton (String)
static char UIB_PROPERTY_KEY;
@dynamic path;
-(void)setPath:(NSDictionary *)attributes {
objc_setAssociatedObject(self, &UIB_PROPERTY_KEY, path, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(NSString *)path {
return (NSString*)objc_getAssociatedObject(self, &UIB_PROPERTY_KEY);
}
@end
答案 1 :(得分:0)
创建一个模型类,该类具有必要的方法和属性来保存数据,添加,删除和获取数据。模型类可以是单例,以便在必要时允许应用程序进行广泛访问。
视图应该仅用于显示或获取用户输入。