通过AFNetworking& amp;编辑它

时间:2015-09-16 21:44:56

标签: ios objective-c uiimageview uiimage afnetworking

我正在尝试通过AFNetworking(UIImageView + AFNetworking.h)从远程服务器下载一些图像文件。从中检索UIImage,编辑该图像。编辑意味着将该图像(下载的图像)添加到另一个png文件之上。(背景图像 - UIImage)

我最后尝试了几个代码块,我被困在这里。我只买了一个黑盒子。无法看到实际的服务器映像。

-(UIImage *)downloadImages:(NSString *)url{

     UIImageView *downloadedImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,40,40)];

     [downloadedImageView setImageWithURL:[NSURL URLWithString:url]
                    placeholderImage:[UIImage imageNamed:@"Loading_image"]];

     UIGraphicsBeginImageContextWithOptions(downloadedImageView.bounds.size, downloadedImageView.opaque, 0.0);
    [downloadedImageView.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return img;
}

然后我在for循环中调用该函数。

for( int i=0;i<[Images count];i++){

  NSString *image_Url = [NSString stringWithFormat:@"%@%@",imageURL, imagename];  
  UIImage *downloadimage =[[UIImage alloc]init];

  downloadimage = [self downloadImages:image_Url];

  UIImage *bottomImage = [UIImage imageNamed:@"map_marker_black"];

  CGSize newSize = CGSizeMake(60, 60);
  UIGraphicsBeginImageContext( newSize );

  [backgroundImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

  [downloadimage drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:0.7];

  UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

  UIGraphicsEndImageContext();

}

我在哪里做错了?非常感谢您的帮助。非常感谢

1 个答案:

答案 0 :(得分:0)

一些问题。让我们从下载代码开始。没有理由处理图像视图并将其绘制为创建图像。只需使用实际图像:

- (UIImage *)downloadImage:(NSString *)url{
     NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
     UIImage *image = [UIImage imageWithData:imageData];

    return image;
}

原始代码的一个大问题是您使用的AFNetworking方法在后台运行。所以你总是试图绘制“加载”图像。

现在绘图代码:

// Only need to load this once
UIImage *backgroundImage = [UIImage imageNamed:@"map_marker_black"];

// What is the loop used for?
for (NSUInteger i = 0; i < [Images count]; i++){
    // Where does imageURL and imagename come from?
    NSString *imageURL = [NSString stringWithFormat:@"%@%@",imageURL, imagename];
    UIImage *downloadImage = [self downloadImage:imageURL];

    CGRect newRect = CGRectMake(0, 0, 60, 60);
    UIGraphicsBeginImageContextWithOptions(newRect.size, NO, 0.0);

    [backgroundImage drawInRect:newRect];

    [downloadimage drawInRect:newRect blendMode:kCGBlendModeNormal alpha:0.7];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    // Do something here with newImage

    UIGraphicsEndImageContext();
}

注意代码中的注释。