gpt4 book ai didi

ios - 我如何使用 Assets url 获取图像或视频文件的 NSData

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:06:12 31 4
gpt4 key购买 nike

我有一个像

这样的网址

"assets-library://asset/asset.PNG?id=2B9DB56C-B9C6-4F4E-AD51-8A5E5F1DD2AA&ext=PNG"

在“图像”数组中。我如何通过这个 url 从媒体获取 NSData?我需要在此代码中使用 NSData:

#pragma mark VK methods

+(NSMutableArray*)attachmentIds:(NSArray*)images forMe:(NSDictionary*)me{
NSMutableArray *attachmentsList = [NSMutableArray new];

for (int i = 0; i<images.count; i++){

NSData *imageData = [NSData dataWithContentsOfURL:[images objectAtIndex:i] ];

NSString *serverUrl = [self getServerForVKUploadPhotoToWall:me];
NSDictionary *uploadResult = [self sendVKPOSTRequest:serverUrl withImageData:imageData];
NSString *hash = [uploadResult objectForKey:@"hash"];
NSString *photo = [uploadResult objectForKey:@"photo"];
NSString *server = [uploadResult objectForKey:@"server"];
NSString *attach_id = [self getVKAttachIdforUser:me photo:photo server:server hash:hash];
[attachmentsList addObject:attach_id];
}
return attachmentsList;
}

但是 NSData *imageData = [NSData dataWithContentsOfURL:[images objectAtIndex:i]];不工作;

最佳答案

我解决了我的问题。谢谢https://www.cocoacontrols.com/controls/doimagepickercontroller

- (NSData *)getCroppedData:(NSURL *)urlMedia
{
__block NSData *iData = nil;
__block BOOL bBusy = YES;

ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
ALAssetRepresentation *representation = myasset.defaultRepresentation;
long long size = representation.size;
NSMutableData *rawData = [[NSMutableData alloc] initWithCapacity:size];
void *buffer = [rawData mutableBytes];
[representation getBytes:buffer fromOffset:0 length:size error:nil];
iData = [[NSData alloc] initWithBytes:buffer length:size];
bBusy = NO;
};

ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror)
{
NSLog(@"booya, cant get image - %@",[myerror localizedDescription]);
};

[_assetsLibrary assetForURL:urlMedia
resultBlock:resultblock
failureBlock:failureblock];

while (bBusy)
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];

return iData;
}

关于ios - 我如何使用 Assets url 获取图像或视频文件的 NSData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22193322/

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