美文网首页
获取图片

获取图片

作者: 863cda997e42 | 来源:发表于2017-12-25 11:02 被阅读8次
    + (UIImage *) getImageFromURL:(NSString *)fileURL {
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
        UIImage *result = [UIImage imageWithData:data scale:[[UIScreen mainScreen] scale]];
        
        return result;
    }
    
    + (void) saveImage:(UIImage *)image withFileName:(NSString *)imageName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {
        if ([[extension lowercaseString] isEqualToString:@"png"]) {
            [UIImagePNGRepresentation(image) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"png"]] options:NSAtomicWrite error:nil];
        } else if ([[extension lowercaseString] isEqualToString:@"jpg"] || [[extension lowercaseString] isEqualToString:@"jpeg"]) {
            [UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil];
        } else {
            NSLog(@"文件后缀不认识");
        }
    }
    
    + (UIImage *) loadImage:(NSString *)fileName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {
        NSData *data = [NSData dataWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.%@", directoryPath, fileName, extension]];
        UIImage *result = [UIImage imageWithData:data scale:[[UIScreen mainScreen] scale]];
    
        return result;
    }
    
    
    

    相关文章

      网友评论

          本文标题:获取图片

          本文链接:https://www.haomeiwen.com/subject/ypxlgxtx.html