我正在编写一个应用程序,我需要将图像发送PNG
到JPG
服务器。将 JPEG/PNG 图像转换为 Base64 字符串的代码,我编写为 Obj-C 块。但是当我尝试将块返回的字符串添加到NSDictionary
我得到code=EXC_ARM_DA_ALIGN
. 在我的项目中,我使用 ARC。我阅读了有关此问题
ARC 的几篇文章:EXC_BAD_ACCESS 当从块内部调用方法时,在委托方法内部所以我使用copy
!但这对我没有帮助。在我使用之前copy
,我得到EXC_BAD_EXCEPTION
了代码=1,现在在标题上加上了代码。这是我的代码:
__block NSUInteger image_size;
//Obj-C block where PNG or JPEG image converts to Base64 string
NSString* (^ToBase64)(UIImage* imageToConvert, NSMutableDictionary* metadata) =
^(UIImage* imageToConvert, NSMutableDictionary* metadata)
{
NSMutableData *rawData = [NSMutableData data];
NSString* encodedImage ;
if ([(NSString*)[metadata objectForKey:@"extension"] isEqualToString:@"PNG"])
{
[rawData setData:UIImagePNGRepresentation(imageToConvert)];
[Base64 initialize];
encodedImage = [Base64 encode:rawData];
image_size = [rawData length];
}
else if ([(NSString*)[metadata objectForKey:@"extension"] isEqualToString:@"JPG"])
{
[rawData setData:UIImageJPEGRepresentation(imageToConvert, 1.0)];
[Base64 initialize];
encodedImage = [Base64 encode:rawData];
image_size = [rawData length];
}
return encodedImage;
} ;
//Initialize dictionary with image data and metadata to send
int iteration = 0;
for (UIImage* currentEntry in imagesToSent)
{
NSMutableDictionary* currentImageMetadata = (NSMutableDictionary*)[imagesMetadata objectAtIndex:iteration];
[uploadMessage setObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:
[ToBase64(currentEntry,currentImageMetadata) copy],@"image_data", //here I got an exception
[currentImageMetadata objectForKey:@"extension"], @"extension",
[currentImageMetadata objectForKey:@"image_name" ], @"image_name", image_size, @"size", nil]
forKey:[NSString stringWithFormat:@"image%d",iteration]];
iteration++;
}