gpt4 book ai didi

ios - 从 Assets 库的图像 url 代表转到 base64

转载 作者:行者123 更新时间:2023-11-29 10:49:53 24 4
gpt4 key购买 nike

我最终试图将字典中的一组照片以 url rep 形式转换为 base64,以便通过 json 发送。

这是字典代码和它的日志:

    NSDictionary *dict = [self.form dictionaryWithValuesForKeys:keys];
NSLog(@"dict::%@",dict);

NS日志:

dict::{   
boardLodgingFurnished = "<null>";
caption = "<null>";
cars = "";
photos = (
{
caption = "";
urlRep = "assets-library://asset/asset.JPG?id=CE8A426B-3B59-4172-8761-CC477F3BB3EE&ext=JPG";
},
{
caption = "";
urlRep = "assets-library://asset/asset.JPG?id=F4B68A42-1CA0-4880-9FB5-177CB091A28C&ext=JPG";
}
);
yearsAtLocation = "";
yearsInTheBusiness = "";
}

因此,对于字典中的每张照片,我想获取 urlRep 并将其转换为 base64 字符串,并在字典中用它替换 urlRep。

我现在所拥有的..不确定我是否朝着正确的方向前进:

for (id imageURL in [dict objectForKey:@"photos"])
{
ALAssetsLibrary *library = [ALAssetsLibrary new];
ALAsset *ourAsset = [self assetForURL:imageURL withLibrary:library];

/* Check out ALAssets */
NSLog(@"%@", ourAsset);

ALAssetRepresentation *representation = [ourAsset defaultRepresentation];
CGImageRef imageRef = [representation fullResolutionImage];

//TODO: Deal with JPG or PNG
NSData *imageData = UIImageJPEGRepresentation([UIImage imageWithCGImage:imageRef], 0.1);
NSLog(@"imagedata??%@", [imageData base64EncodedString]);
//need to know how to add this back to dict
}

下面的方法是从上面调用的,但是在 while 循环中崩溃了

-[__NSDictionaryI scheme]: unrecognized selector sent to instance 0x166dd090
2014-01-03 10:57:27.361 Inspection App[2728:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI scheme]: unrecognized selector sent to instance 0x166dd090'

方法

- (ALAsset *)assetForURL:(NSURL *)url withLibrary:(ALAssetsLibrary *)assetsLibrary {
__block ALAsset *result = nil;
__block NSError *assetError = nil;
dispatch_semaphore_t sema = dispatch_semaphore_create(0);

[assetsLibrary assetForURL:url resultBlock:^(ALAsset *asset) {
result = asset;
dispatch_semaphore_signal(sema);
} failureBlock:^(NSError *error) {
assetError = error;
dispatch_semaphore_signal(sema);
}];


if ([NSThread isMainThread]) {
while (!result && !assetError) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
}
else {
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}

return result;
}

编辑:

if (photoUrls.count) {
for (id photos in photoUrls){
NSString *urlString = photos;
[self base64ImageAtUrlString:urlString result:^(NSString *base64) {

NSLog(@"imagedata??%@", base64);
}];
}
}
else {
NSLog(@"where are my urls?");
}

NSMutableDictionary *jsonWithPhotos = [dict mutableCopy];

[jsonWithPhotos setObject:convertedImages forKey:@"photo64"];
NSLog(@"jjson photos::%@", jsonWithPhotos);

更新方法

- (void)base64ImageAtUrlString:(NSString *)urlString result:(void (^)(NSString *))completion {
NSURL *url = [NSURL URLWithString:urlString];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

[library assetForURL:url resultBlock:^(ALAsset *asset) {

// borrowing your code, here... didn't check it....
ALAssetRepresentation *representation = [asset defaultRepresentation];
CGImageRef imageRef = [representation fullResolutionImage];

//TODO: Deal with JPG or PNG
NSData *imageData = UIImageJPEGRepresentation([UIImage imageWithCGImage:imageRef], 0.1);
NSString *base64 = [imageData base64EncodedString];
completion(base64);
[convertedImages addObject:base64];

// NSLog(@"converted::%@",convertedImages);

} failureBlock:^(NSError *error) {
NSLog(@"that didn't work %@", error);
}];
}

当我记录 jsonWithPhotos 时,对象 photo64 只是一个空白数组

最佳答案

崩溃是由于代码中关于字典的错误假设造成的。鉴于已发布的字典解析为 json 的描述,您需要像这样获取 url:

// collect the photo urls in an array
NSMutableArray *photoUrls = [NSMutableArray array];

// photos is an array of dictionaries in the dictionary
NSArray *photos = dict[@"photos"];
for (NSDictionary *photo in photos) {
// photo is a dictionary containing a "caption" and a "urlRep"
[photoUrls addObject:photo[@"urlRep"]];
}

现在您可以继续执行其工作只是转换的方法。您的问题可能包含更多有关如何执行此操作的问题。我建议从简单开始。看看你是否可以进行一次转换。通过从 base64 写回图像来测试它。

编辑 0:在不深入检查的情况下,我会将您的编码尝试重组为如下所示:

- (void)base64ImageAtUrlString:(NSString *)urlString result:(void (^)(NSString *))completion {
NSURL *url = [NSURL URLWithString:urlString];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

[library assetForURL:url resultBlock:^(ALAsset *asset) {

// borrowing your code, here... didn't check it....
ALAssetRepresentation *representation = [asset defaultRepresentation];
CGImageRef imageRef = [representation fullResolutionImage];

//TODO: Deal with JPG or PNG
NSData *imageData = UIImageJPEGRepresentation([UIImage imageWithCGImage:imageRef], 0.1);
NSString *base64 = [imageData base64EncodedString];
completion(base64);

} failureBlock:^(NSError *error) {
NSLog(@"that didn't work %@", error);
}];
}

这样调用它:

if (photoUrls.count) {
NSString *urlString = photoUrls[0];
[self base64ImageAtUrlString:urlString result:^(NSString *base64) {
NSLog(@"imagedata??%@", base64);
}];
} else {
NSLog(@"where are my urls?");
}

一旦它开始工作,看看你是否可以反转它,用 base64 数据制作一个图像。最后,一旦一切正常,您就可以处理潜在的内存问题。我的建议是考虑一次编码一个,一次将一个发送到服务器,然后释放其间的所有内容。

编辑 1 - 每个后续问题,如果您想用 base64 编码替换 url 数组中的所有 url,它可能会像这样(请记住,这可能会占用大量内存):

- (void)base64ImagesAtUrls:(NSMutableArray *)urls result:(void (^)(void))completion {

__block NSInteger completed = 0; // this is how we'll know that we're done
// this approach doesn't depend on the asset library retrievals completing
// sequentially, even though they probably will

for (int i=0; i<urls.count; i++) {
NSString *urlString = urls[i];
[self base64ImageAtUrlString:urlString result:^(NSString *base64) {
[urls replaceObjectAtIndex:i withObject:base64];
if (++completed == urls.count) completion();
}];
}
}

关于ios - 从 Assets 库的图像 url 代表转到 base64,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20909094/

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