1

我正在开发一个应用程序,当用户从相机拍照时,我必须一张一张地拼接多张图像。

这就是我用来合并两个图像的方法。

[self performSelector:@selector(joinImages:secondImage:) withObject:firstimage withObject:imageCaptured];

- (UIImage *)joinImages:(UIImage *)im1 secondImage:(UIImage *)im2
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];


    //Joins 2 UIImages together, stitching them horizontally
    CGSize size = CGSizeMake(im1.size.width+im2.size.width, im2.size.height);
    UIGraphicsBeginImageContext(size);

    CGPoint image1Point = CGPointMake(0, 0);
    [im1 drawAtPoint:image1Point];

    CGPoint image2Point = CGPointMake(im1.size.width,0);
    [im2 drawAtPoint:image2Point];

    UIImage* finalImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    firstimage=finalImage;// final images updated everytime

    [pool release];
    return finalImage;
}

但是当我在 iPhone 中运行它时收到内存警告,它适用于 iPod。

如果是 iPhone,图像也会被裁剪。

我可以做任何事情来解决这个问题。

谢谢..

4

2 回答 2

1

你可以做这样的事情。

 - (UIImage *)joinImages:(UIImage *)im1 secondImage:(UIImage *)im2
   {   
    @autoreleasepool
    {
       //Joins 2 UIImages together, stitching them horizontally
       CGSize size = CGSizeMake(im1.size.width+im2.size.width, im2.size.height);
      UIGraphicsBeginImageContext(size);

      CGPoint image1Point = CGPointMake(0, 0);
      [im1 drawAtPoint:image1Point];

      CGPoint image2Point = CGPointMake(im1.size.width,0);
      [im2 drawAtPoint:image2Point];

      UIImage* finalImage = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
      firstimage=finalImage;// final images updated everytime

      return finalImage;
   }   
 }

希望这对你有用。

于 2013-10-22T11:01:58.697 回答
0
 [self joinImages:firstimage secondImage:secondImage];

- (UIImage *)joinImages:(UIImage *)im1 secondImage:(UIImage *)im2
{
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//Joins 2 UIImages together, stitching them horizontally
 CGSize size = CGSizeMake(im1.size.width+im2.size.width, im2.size.height);
 UIGraphicsBeginImageContext(size);

 CGPoint image1Point = CGPointMake(0, 0);
 [im1 drawAtPoint:image1Point];

 CGPoint image2Point = CGPointMake(im1.size.width,0);
 [im2 drawAtPoint:image2Point];

 UIImage* finalImage = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();
 firstimage=finalImage;// final images updated everytime

 [pool release];
 return finalImage;
 }
于 2013-10-22T13:20:48.950 回答