[英]How to compare two ALAssets?
I have some trouble with compare two ALAssets object. 比较两个ALAssets对象有些麻烦。 I have two NSMutableArray: selectedImages and mutableAssets.
我有两个NSMutableArray:selectedImages和mutableAssets。 I store there ALAssets object.
我在那里存储ALAssets对象。 But when i want to compare this assets it doesnt work for isEqual or containsObject method, only when i compare it by their url it works:
但是,当我想比较此资产时,它不适用于isEqual或containsObject方法,只有当我通过其网址比较它时,它才有效:
ALAsset *asset1 = [self.mutableAssets objectAtIndex:0];
ALAsset *asset2 = [self.selectedImages objectAtIndex:0];
NSLog(@"%@", asset1);
NSLog(@"%@", asset2);
if([self.selectedImages containsObject:[self.mutableAssets objectAtIndex:0]]) {
NSLog(@"the same1");
}
if([asset1 isEqual:asset2]) {
NSLog(@"the sames2");
}
if([asset1.defaultRepresentation.url isEqual:asset2.defaultRepresentation.url]) {
NSLog(@"the same3");
}
Gives only this line: 仅给出以下行:
ALAsset - Type:Photo, URLs:assets-library://asset/asset.JPG?id=E8947286-22E2-42E4-A904-14D940A387B3&ext=JPG
ALAsset - Type:Photo, URLs:assets-library://asset/asset.JPG?id=E8947286-22E2-42E4-A904-14D940A387B3&ext=JPG
the same3
Why it happens? 为什么会发生?
seems like Assets don't implement isEqual:
似乎资产未实现
isEqual:
I would not check the defaultRep's URL though... two different assets may have the same defaultRep in a way 我不会检查defaultRep的URL ...两种不同的资产可能在某种程度上具有相同的defaultRep
I'd go with the ALAssetPropertyAssetURL for iOS 6+
or ALAssetPropertyURLs for ios4&5
我会使用
ALAssetPropertyAssetURL for iOS 6+
的ALAssetPropertyAssetURL for iOS 6+
或ALAssetPropertyURLs for ios4&5
--- you could wrap this in a category even! ---您甚至可以将其包装在一个类别中!
@interface ALAsset (isEqual)
- (NSURL*)defaultURL;
@end
@implementation ALAsset (isEqual)
- (NSURL*)defaultURL {
if([[[UIDevice currentDevice] systemVersion] floatValue]>=6.0)
{
return [self valueForKey: ALAssetPropertyAssetURL];
}
else
{
return self.defaultRepresentation.url;
}
}
- (BOOL)isEqual:(id)obj {
if(![obj isKindOfClass:[ALAsset class]])
return NO;
NSURL *u1 = [self defaultURL];
NSURL *u2 = [obj defaultURL];
return ([u1 isEqual:u2]);
}
for iOS 4 and 5 and 6 and up 适用于iOS 4、5和6及更高版本
containsObject:
is determines whether anObject is present in the array by sending an isEqual: message to each of the array's objects (and passing anObject as the parameter to each isEqual: message). containsObject:
是通过向数组的每个对象发送isEqual:消息(并将anObject作为参数传递给每个isEqual:消息)来确定anObject是否存在于数组中。
isEqual:
is inspect whether two objects are the same. isEqual:
检查两个对象是否相同。 If two objects are equal, they must have the same hash
value. 如果两个对象相等,则它们必须具有相同的
hash
值。 hash
means a table address in a hash table structure. hash
表示哈希表结构中的表地址。
so, if not containsObject also not isEqual. 因此,如果没有containsObject也不是isEqual。 Derive the results if you want to your ALAsset override
isEqual
. 如果要对ALAsset覆盖
isEqual
则得出结果。
NSURL, isEqual
is two NSURLs are considered equal if and only if they return identical values for both baseURL and relativeString. NSURL,
isEqual
是两个NSURL,当且仅当它们返回的baseURL和relativeString值相同时,才视为相等。
if([[[asset1 defaultRepresentation].url absoluteString] isEqualToString:[[asset2 defaultRepresentation].url absoluteString]])
{
}
else
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.