ViewController A의 사전에서 URL로 이미지를 가져오고 해당 사전을 ViewController BI에 전달했습니다. BI가 필요합니다. 사용자가 이미지를 업데이트 한 경우 업데이트 된 이미지를 보여줍니다. 그렇지 않으면 이전 이미지를 표시합니다. 이미지와 나는 그것을 위해 다음과 같은 코드를하고있다. 친절하게 확인하고 그것이 원하는대로 작동하지 않고 모든 경우에만 이전 이미지를 보여주는 이유를 말해라.이미지를 업데이트하고 사전에 저장
-(void)showUserImage:(NSURL*)imgUrl
{
[ConnectionManager setSharedCacheForImages];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:imgUrl];
NSURLSession *session = [ConnectionManager prepareSessionForRequest];
NSCachedURLResponse *cachedResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:request];
if (cachedResponse.data) {
UIImage *downloadedImage = [UIImage imageWithData:cachedResponse.data];
dispatch_async(dispatch_get_main_queue(), ^{
_profileImageView.image = downloadedImage;
});
} else {
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSHTTPURLResponse *res = (NSHTTPURLResponse *)response;
if(res.statusCode == 200){
dispatch_async(dispatch_get_main_queue(), ^{
_profileImageView.image = [UIImage imageWithData:data];
});
}
}];
[task resume];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
if(_profileImageView.image == [_detailsDictionary valueForKey:@"ProfilePictureUrl"]) {
NSLog(@"Th url of image is %@",[_detailsDictionary valueForKey:@"ProfilePictureUrl"]);
}
else {
_profileImageView.image = image;
UIImage *updatedImage = _profileImageView.image;
NSData *imageData = UIImageJPEGRepresentation(updatedImage, 100);
NSString *strEncoded = [imageData base64EncodedStringWithOptions:0];
[_detailsDictionary setObject:strEncoded forKey:@"ProfilePictureUrl"];
[self dismissViewControllerAnimated:YES completion:nil];
}
}
이 이미지를 비교하려면, 먼저 URL에서 이미지를 다운로드이있는 NSData의 생성, 해시로 변환해야합니다. 선택한 로컬 이미지와 동일한 작업을 수행하십시오. 이제 해시를 비교해보십시오. 동일하면 이미지가 동일합니다. – NSNoob
또는 서버의 이미지 이름을 이미지의 장치 ID 및 로컬 식별자의 해시로 지정한 다음 그림 URL에 포함시킬 수 있습니다. 선택한 이미지의 localIdentifier 및 장치의 ID에 대해 url의 해시 문자열과 생성 된 해시를 비교합니다. 동일하면 이미지가 동일합니다. 처음에는 이미지를 다운로드하는 번거 로움을 덜어주기 때문에 개인적으로 두 번째 방법을 사용했습니다 – NSNoob