Browse Source

Insert avatar on manageAccount

Marino Faggiana 8 years ago
parent
commit
9d844764c2

+ 21 - 0
Libraries external/APAvatarImageView/APAvatarImageView.h

@@ -0,0 +1,21 @@
+//
+//  APAvatarImageView.h
+//  Avatar
+//
+//  Created by Ankur Patel on 10/19/13.
+//  Copyright (c) 2013 Patel Labs LLC. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+
+@interface APAvatarImageView : UIImageView
+
+@property (nonatomic, retain) UIColor *borderColor;
+@property (nonatomic, assign) float borderWidth;
+@property (nonatomic, assign) float cornerRadius;
+
+- (id)initWithFrame:(CGRect)frame borderColor:(UIColor*)borderColor borderWidth:(float)borderWidth;
+- (id)initWithImage:(UIImage *)image borderColor:(UIColor*)borderColor borderWidth:(float)borderWidth;
+- (id)initWithImage:(UIImage *)image highlightedImage:(UIImage *)highlightedImage borderColor:(UIColor*)borderColor borderWidth:(float)borderWidth;
+
+@end

+ 115 - 0
Libraries external/APAvatarImageView/APAvatarImageView.m

@@ -0,0 +1,115 @@
+//
+//  APAvatarImageView.m
+//  Avatar
+//
+//  Created by Ankur Patel on 10/19/13.
+//  Copyright (c) 2013 Patel Labs LLC. All rights reserved.
+//
+
+#import "APAvatarImageView.h"
+
+@interface APAvatarImageView ()
+
+- (void)draw;
+
+@end
+
+@implementation APAvatarImageView
+
+- (id)initWithFrame:(CGRect)frame
+{
+    self = [super initWithFrame:frame];
+    if (self) {
+        _cornerRadius = self.frame.size.height/2.0f;
+        [self draw];
+    }
+    return self;
+}
+
+- (id)initWithCoder:(NSCoder *)aDecoder
+{
+    self = [super initWithCoder:aDecoder];
+    if (self) {
+        _borderWidth = -1.0;
+        _cornerRadius = self.frame.size.height/2.0f;
+        [self draw];
+    }
+    return self;
+}
+
+- (id)initWithFrame:(CGRect)frame borderColor:(UIColor*)borderColor borderWidth:(float)borderWidth
+{
+    self = [super initWithFrame:frame];
+    if (self) {
+        _borderColor = borderColor;
+        _borderWidth = borderWidth;
+        _cornerRadius = self.frame.size.height/2.0f;
+        [self draw];
+    }
+    return self;
+}
+
+- (id)initWithImage:(UIImage *)image borderColor:(UIColor*)borderColor borderWidth:(float)borderWidth
+{
+    self = [super initWithImage:image];
+    if (self) {
+        _borderColor = borderColor;
+        _borderWidth = borderWidth;
+        _cornerRadius = self.frame.size.height/2.0f;
+        [self draw];
+    }
+    return self;
+}
+
+- (id)initWithImage:(UIImage *)image highlightedImage:(UIImage *)highlightedImage borderColor:(UIColor*)borderColor borderWidth:(float)borderWidth
+{
+    self = [super initWithImage:image highlightedImage:highlightedImage];
+    if (self) {
+        _borderColor = borderColor;
+        _borderWidth = borderWidth;
+        _cornerRadius = self.frame.size.height/2.0f;
+        [self draw];
+    }
+    return self;
+}
+
+- (void)setBorderColor:(UIColor *)borderColor
+{
+    _borderColor = borderColor;
+    [self draw];
+}
+
+- (void)setBorderWidth:(float)borderWidth
+{
+    _borderWidth = borderWidth;
+    [self draw];
+}
+
+-(void)setCornerRadius:(float)cornerRadius
+{
+    _cornerRadius = cornerRadius;
+    [self draw];
+}
+
+- (void)draw
+{
+    CGRect frame = self.frame;
+    if (frame.size.width != frame.size.height) {
+        NSLog(@"Warning: Height and Width should be the same for image view");
+    }
+    CALayer *l = [self layer];
+    [l setMasksToBounds:YES];
+    [l setCornerRadius:_cornerRadius];
+    if (_borderWidth < 0) { // Default case
+        [l setBorderWidth:3.0];
+    } else {
+        [l setBorderWidth:_borderWidth];
+    }
+    if (_borderColor == nil) {
+        [l setBorderColor:[[UIColor lightGrayColor] CGColor]];
+    } else {
+        [l setBorderColor:[_borderColor CGColor]];
+    }
+}
+
+@end

+ 14 - 0
Nextcloud.xcodeproj/project.pbxproj

@@ -7,6 +7,7 @@
 	objects = {
 
 /* Begin PBXBuildFile section */
+		F705723E1EA8DC4E00268E43 /* APAvatarImageView.m in Sources */ = {isa = PBXBuildFile; fileRef = F705723D1EA8DC4E00268E43 /* APAvatarImageView.m */; };
 		F708CF6D1E56E8CC00271D8B /* TableMetadata+CoreDataClass.m in Sources */ = {isa = PBXBuildFile; fileRef = F708CF4E1E56E8CB00271D8B /* TableMetadata+CoreDataClass.m */; };
 		F708CF6E1E56E8CC00271D8B /* TableMetadata+CoreDataClass.m in Sources */ = {isa = PBXBuildFile; fileRef = F708CF4E1E56E8CB00271D8B /* TableMetadata+CoreDataClass.m */; };
 		F708CF6F1E56E8CC00271D8B /* TableMetadata+CoreDataClass.m in Sources */ = {isa = PBXBuildFile; fileRef = F708CF4E1E56E8CB00271D8B /* TableMetadata+CoreDataClass.m */; };
@@ -755,6 +756,8 @@
 		F70211FB1BAC56E9003FC03E /* CCMain.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCMain.m; sourceTree = "<group>"; };
 		F7053E3C1C639DF500741EA5 /* CCUtility.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCUtility.h; sourceTree = "<group>"; };
 		F7053E3D1C639DF500741EA5 /* CCUtility.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCUtility.m; sourceTree = "<group>"; };
+		F705723C1EA8DC4E00268E43 /* APAvatarImageView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = APAvatarImageView.h; sourceTree = "<group>"; };
+		F705723D1EA8DC4E00268E43 /* APAvatarImageView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = APAvatarImageView.m; sourceTree = "<group>"; };
 		F706C5031EA61F54003A48A3 /* es-MX */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "es-MX"; path = "es-MX.lproj/CTAssetsPicker.strings"; sourceTree = "<group>"; };
 		F706C5041EA61F54003A48A3 /* es-MX */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "es-MX"; path = "es-MX.lproj/SwiftWebVC.strings"; sourceTree = "<group>"; };
 		F706C5051EA61F54003A48A3 /* es-MX */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "es-MX"; path = "es-MX.lproj/BKPasscodeView.strings"; sourceTree = "<group>"; };
@@ -1753,6 +1756,15 @@
 			path = Main;
 			sourceTree = "<group>";
 		};
+		F705723B1EA8DC4E00268E43 /* APAvatarImageView */ = {
+			isa = PBXGroup;
+			children = (
+				F705723C1EA8DC4E00268E43 /* APAvatarImageView.h */,
+				F705723D1EA8DC4E00268E43 /* APAvatarImageView.m */,
+			);
+			path = APAvatarImageView;
+			sourceTree = "<group>";
+		};
 		F70784811A2C8A0D00AC9FFF /* UploadFromOtherUpp */ = {
 			isa = PBXGroup;
 			children = (
@@ -1768,6 +1780,7 @@
 			children = (
 				F70F02A91C889183008DAB36 /* AESCrypt-ObjC */,
 				F70F02B21C889183008DAB36 /* AFViewShaker */,
+				F705723B1EA8DC4E00268E43 /* APAvatarImageView */,
 				F7F06E291DBFACC600099AE9 /* CTAssetsPickerController */,
 				F73CCE221DC13788007E38D8 /* DZNEmptyDataSet */,
 				F7659A211DC0B726004860C4 /* EAIntroView */,
@@ -4175,6 +4188,7 @@
 				F73CCDFD1DC13776007E38D8 /* XLFormViewController.m in Sources */,
 				F73CCDDF1DC13776007E38D8 /* XLFormDateCell.m in Sources */,
 				F73CCE1B1DC13776007E38D8 /* XLFormValidationStatus.m in Sources */,
+				F705723E1EA8DC4E00268E43 /* APAvatarImageView.m in Sources */,
 				F77B0E441D118A16002130FE /* CCIntro.m in Sources */,
 				F77B0E451D118A16002130FE /* ReaderThumbRender.m in Sources */,
 				F77B0E481D118A16002130FE /* CCCrypto.m in Sources */,

+ 1 - 0
iOSClient/Settings/CCManageAccount.h

@@ -25,6 +25,7 @@
 #import "XLForm.h"
 #import "CCLogin.h"
 #import "CCCoreData.h"
+#import "APAvatarImageView.h"
 
 @interface CCManageAccount : XLFormViewController <CCLoginDelegate>
 

+ 52 - 5
iOSClient/Settings/CCManageAccount.m

@@ -374,13 +374,22 @@
     pickerAccount.rowDescriptor.value = app.activeAccount;
     
     UIImage *avatar = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/avatar.png", app.directoryUser]];
+    if (!avatar)
+        avatar = [UIImage imageNamed:@"avatar"];
     
-    if (avatar)
-        [pickerAccount.rowDescriptor.cellConfig setObject:avatar forKey:@"imageView.image"];
-    else
-        [pickerAccount.rowDescriptor.cellConfig setObject:nil forKey:@"imageView.image"];
-
+    avatar = [self imageWithImage:avatar scaledToSize:CGSizeMake(40, 40) isAspectRation:YES];
     
+    APAvatarImageView *avatarImageView = [[APAvatarImageView alloc] initWithImage:avatar borderColor:[UIColor lightGrayColor] borderWidth:0.5];
+        
+    CGSize imageSize = avatarImageView.bounds.size;
+    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
+    CGContextRef context = UIGraphicsGetCurrentContext();
+    [avatarImageView.layer renderInContext:context];
+    avatar = UIGraphicsGetImageFromCurrentImageContext();
+    UIGraphicsEndImageContext();
+        
+    [pickerAccount.rowDescriptor.cellConfig setObject:avatar forKey:@"imageView.image"];
+
     // --
     
      _tableAccount = [CCCoreData getActiveAccount];
@@ -426,4 +435,42 @@
     [self.tableView reloadData];
 }
 
+- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize isAspectRation:(BOOL)aspect {
+    if (!image) {
+        return nil;
+    }
+    //UIGraphicsBeginImageContext(newSize);
+    // In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
+    // Pass 1.0 to force exact pixel size.
+    CGFloat originRatio = image.size.width / image.size.height;
+    CGFloat newRatio = newSize.width / newSize.height;
+    
+    CGSize sz;
+    
+    if (!aspect) {
+        sz = newSize;
+    }else {
+        if (originRatio < newRatio) {
+            sz.height = newSize.height;
+            sz.width = newSize.height * originRatio;
+        }else {
+            sz.width = newSize.width;
+            sz.height = newSize.width / originRatio;
+        }
+    }
+    CGFloat scale = 1.0;
+    //    if([[UIScreen mainScreen]respondsToSelector:@selector(scale)]) {
+    //        CGFloat tmp = [[UIScreen mainScreen]scale];
+    //        if (tmp > 1.5) {
+    //            scale = 2.0;
+    //        }
+    //    }
+    sz.width /= scale;
+    sz.height /= scale;
+    UIGraphicsBeginImageContextWithOptions(sz, NO, scale);
+    [image drawInRect:CGRectMake(0, 0, sz.width, sz.height)];
+    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
+    UIGraphicsEndImageContext();
+    return newImage;
+}
 @end