我试图从一个实际上保存在NSString中的字节数组创建一个UIImage.
有人可以告诉我如何做到这一点?
这是我在想的事情:
NSString *sourceString = @"mYActualBytesAREinHERe=";
//get the bytes
const char *bytesArray = [sourceString cStringUsingEncoding:NSASCIIStringEncoding];
//build the NSData object
NSData *data = [NSData dataWithBytes:bytesArray length:[sourceString length]];
//create actual image
UIImage *image = [UIImage imageWithData:data];
Run Code Online (Sandbox Code Playgroud)
问题是图像总是0x0(零).
任何建议,将不胜感激.
谢谢!
我的目标是在视图中显示图像.考虑到我有一个:
在imageData中存储了图像(我使用了initWithContentsOfFile方法).
现在,如果我用以下内容初始化imageView:
NSImage *imageView = [[NSImage alloc] initWithContentsOfFile:path];
Run Code Online (Sandbox Code Playgroud)
我可以正确地看到图像的渲染,但是这样我就从文件系统中读取了两次.
如果我尝试:
NSImage *imageView = [[NSImage alloc] initWithData:imageData];
Run Code Online (Sandbox Code Playgroud)
显示的图像非常小..像拇指
Whit CGImageRef:
CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)imageData, NULL);
CGImageRef imageRef= CGImageSourceCreateImageAtIndex(imageSource, 0, nil);
NSImage *imageView = [[NSImage alloc] initWithCGImage:imageRef size:NSZeroSize];
Run Code Online (Sandbox Code Playgroud)
图像仍然渲染得太小.
如何以原始分辨率显示图像?
我试图将sqlite中的一些图像缓存为nsdata,当我尝试使用sqlite3_exec和原始SQL字符串(作为NSString)插入字节数组时,我遇到了问题
NSData *imgData = UIImagePNGRepresentation(img);
NSString* sql = [NSString stringWithFormat:@"INSERT INTO persistedimg (imgx,idvalx) VALUES (%@,'%@')", imgData, idValue];
rc = sqlite3_exec(db, [sql UTF8String], callbackFunction, (void*)contextObject, &zErrMsg);
Run Code Online (Sandbox Code Playgroud)
但是上面的问题是我将NSData直接添加到sql字符串而不是字节.
我想做这样的事情
... [imgData bytes], [imgData length]
Run Code Online (Sandbox Code Playgroud)
但是因为我没有使用典型的"_bind_blob"方法,所以我不知道如何用原始字符串做
更新
我正在使用一个我想要粘贴的包装器,只需编写一个新方法来支持图像插入/查询命令
到目前为止,下面是我的整个包装类
**
#import "SQLiteAccess.h"
#import <sqlite3.h>
@implementation SQLiteAccess
+ (NSString *)pathToDB {
NSString *dbName = @"test123";
NSString *originalDBPath = [[NSBundle mainBundle] pathForResource:dbName ofType:@"db"];
NSString *path = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *appSupportDir = [paths objectAtIndex:0];
NSString *dbNameDir = [NSString stringWithFormat:@"%@/test123", appSupportDir];
NSFileManager …Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我试图序列化服务器响应字典并将其写入文件系统.但是我收到一些回复的错误"属性列表格式无效".原因是服务器响应中的CFNull对象.现在,服务器响应将继续改变,所以我没有明确的方法来删除CFNull对象().以下是我的代码:
NSString *anError = nil;
NSData *aData = [NSPropertyListSerialization dataFromPropertyList:iFile format:NSPropertyListXMLFormat_v1_0 errorDescription:&anError];
Run Code Online (Sandbox Code Playgroud)
解决这个问题的最佳方法是什么?如何一次性从服务器响应中删除所有CFNull对象?
我必须在iPhone应用程序内验证应用程序内购买的收据(仅限iOS 7).
不幸的是,加密,openssl和In-App购买对我来说是全新的,所以我有一些问题让他们工作.
我正在按照Apple提供的指南在本地验证收据,我在我的项目中将openssl作为静态库包含在内.这是Apple提供的用于使用OpenSSL验证收据签名的代码:
BIO *b_receipt;
BIO *b_x509;
PKCS7 *p7 = d2i_PKCS7_bio(b_receipt, NULL);
X509_STORE *store = X509_STORE_new();
X509 *appleRootCA = d2i_X509_bio(b_x509, NULL);
X509_STORE_add_cert(store, appleRootCA);
BIO *b_receiptPayload;
int result = PKCS7_verify(p7, NULL, store, NULL, b_receiptPayload, 0);
if (result == 1)
{
// Receipt signature is Valid
// b_receiptPayload contains the payload
}
Run Code Online (Sandbox Code Playgroud)
我使用此代码获取收据和证书:
NSData *receiptData = [NSData dataWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]];
NSData *certificateData = [NSData dataWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"AppleIncRootCertificate" withExtension:@"cer"]];
Run Code Online (Sandbox Code Playgroud)
我如何使用这两种NSData初始化BIO变量b_receipt和b_x509?
我与AFNetworking 1.0进行了这个调用,它返回了一个responseObject包含我想要的API的数据:
[[AFDiffbotClient sharedClient] postPath:@"http://diffbot.com/api/batch" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
Run Code Online (Sandbox Code Playgroud)
但是,我不知道如何处理responseObject.
如果我检查[responseObject class]我得到NSData.
如果我NSLog(@"%@", responseObject)得到一堆数字(我假设的内存地址):
<5b0a7b22 68656164 65727322 3a5b7b22 6e616d65 223a6e75 6c6c2c22 76616c75 65223a22 48545450 2f312e31 20323030 204f4b22 7d2c7b22 6e616d65 223a2244 61746522 2c227661 ...
如果我做:
NSString *responseString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"%@", responseString);
Run Code Online (Sandbox Code Playgroud)
我得到了我想要的输出!但是,这是一个NSString.
如果我做:
NSError *error;
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:&error];
NSLog(@"%@", responseDictionary);
Run Code Online (Sandbox Code Playgroud)
我得到了一个NSDictionary,但它缺少绝大多数的响应(即:我没有得到该NSString方法所包含的内容).
我该如何处理这个对象?
我使用此代码将一些PDF数据保存到文件中,使用"打开方式"菜单将其发送到另一个应用程序,然后在完成后删除该文件:
- (void)openIn:(NSData *)fileData {
// save the PDF data to a temporary file
NSString *fileName = [NSString stringWithFormat:@"%@.pdf", self.name];
NSString *filePath = [NSString stringWithFormat:@"%@/Documents/%@", NSHomeDirectory(), fileName];
BOOL result = [fileData writeToFile:filePath atomically:TRUE];
if (result) {
NSURL *URL = [NSURL fileURLWithPath:filePath];
UIDocumentInteractionController *controller = [[UIDocumentInteractionController interactionControllerWithURL:URL] retain];
controller.delegate = self;
[controller presentOpenInMenuFromBarButtonItem:self.openInButton animated:TRUE];
}
}
- (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller {
// when the document interaction controller finishes, delete the temporary file
NSString *fileName = [NSString stringWithFormat:@"%@.pdf", self.name];
NSString *filePath = [NSString …Run Code Online (Sandbox Code Playgroud) 该方法enumerateByteRangesUsingBlock:在课堂上NSData,并在Apple文档中解释如下:
使用块枚举数据对象中的每个字节范围.
- (void)enumerateByteRangesUsingBlock:(void (^)(const void *bytes, NSRange byteRange, BOOL *stop))block参数
block
要应用于数组中字节范围的块.该块有三个参数:
bytes
当前范围的字节.
byteRange
当前数据字节的范围.
stop
对布尔值的引用.该块可以将值设置为YES以停止进一步处理数据.stop参数是一个out-only参数.您应该只在块中将此布尔值设置为YES.讨论
枚举块被调用一次用于接收器中的每个连续内存区域(对于连续的NSData对象一次),直到枚举所有字节或者将stop参数设置为YES.
但我的问题是,块何时执行?哪个方法负责提供参数bytes,byteRange以及stop块?例如,如果我想遍历一部分字节数组,我该怎么做才能控制?
有什么区别
+ (instancetype)dataWithBytes:(const void *)bytes length:(NSUInteger)length;
和
+ (instancetype)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length;
也,
+ (instancetype)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length freeWhenDone:(BOOL)b;
if b == YES,转换成数据后会自动释放字节吗?
我正在开发一个应用程序,几乎完成了它.但最后一个问题是它在设备上运行时因内存错误而崩溃.它只在设备上崩溃,但在模拟器中它是完美的.
"malloc: * error for object 0x17415d0c0: Invalid pointer dequeued from free list * set a breakpoint in malloc_error_break to debug";
Run Code Online (Sandbox Code Playgroud)
我已经在这个问题上工作了好几天: iOS - 我的应用程序崩溃了内存错误,如果只在Device上运行
但最后我发现问题,在我的加密和解密功能中,我有这个:
Byte *buffer = (Byte*)malloc(asciiDataLength);
Run Code Online (Sandbox Code Playgroud)
在我使用缓冲区处理后,我将其转换为NSData:
NSData *plainData = [NSData dataWithBytesNoCopy:buffer length:asciiDataLength freeWhenDone:YES];
Run Code Online (Sandbox Code Playgroud)
此代码导致我的应用程序不断崩溃,我将其更改为
NSData *plainData = [NSData dataWithBytes:buffer length:asciiDataLength];
free(buffer);
Run Code Online (Sandbox Code Playgroud)
然后我的应用程序再也不会崩溃
所以,我必须自己解除Byte,ARC不会为我释放它.
我有一个tableView在单元格中显示图像,我正在viewDidLoad函数中获取数据
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableview.dequeueReusableCellWithIdentifier("cardSelectionCell", forIndexPath: indexPath) as! MyCell
let card = fetchedResultsController.objectAtIndexPath(indexPath) as! Card
cell.Name?.text = card.name
var image: NSData = card.photo as NSData
cell.logo.image = UIImage(data: image)
let date = NSDate()
let formatter = NSDateFormatter()
formatter.timeStyle = .MediumStyle
cell.policyExpiryDate.text = formatter.stringFromDate(date)
return cell
}
Run Code Online (Sandbox Code Playgroud)
但问题是,当我开始滚动时,tableView非常滞后所以我试图创建一个字典来转换viewDidLoad()中的图像
var imageDictionary = [Card:UIImage]()
AllCards = context.executeFetchRequest(request, error: nil) as! [Card]
for card in AllCards { …Run Code Online (Sandbox Code Playgroud) nsdata ×10
ios ×6
objective-c ×6
iphone ×2
afnetworking ×1
byte ×1
cgimage ×1
cocoa-touch ×1
filesystems ×1
image ×1
json ×1
nsdictionary ×1
nsimage ×1
nsview ×1
openssl ×1
sqlite ×1
swift ×1
uiimage ×1
uitableview ×1